字符串的指针数组(指针的基本类型) [英] Array of pointers to strings(base type of pointers)

查看:111
本文介绍了字符串的指针数组(指针的基本类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main(){
int i;
char *names[]={"pankaj",
               "anshul",
               "dheeraj",
               "rahul"
               };
for(i=0;i<4;i++)
    printf("\n%u",names[i]);}

当我对此进行编码时,我发现所有的字符串都以连续的方式排列.对于前.如果names [0] = 436,然后names [1] = 443,names [2] = 450,names [3] = 458,则所有字符串都以连续的方式分配了内存,其中\ 0作为字符. 但是可以在任何位置为字符串分配内存,并且它们的基地址存储在指向这些字符串的指针数组中.不应该那样吗? 此外,声明的数组中这些指针的基本类型是什么?它们是指向char的指针还是指向字符串的指针.**就像这些** char(* p)[6] 类型的指针数组或 char * p 类型的指针. 我们可以使用以下名称数组来处理整个字符串:

int main()
{
char *names[]={"pankaj",
               "anshul",
               "dheeraj",
               "rahul"
               };
char *temp;
printf("Original:%s%s\n",names[2]names[3]);
temp=names[2];
names[2]=names[3];
names[3]=temp;
printf("New:%s%s",names[2],names[3]);
return 0;
}

在这种情况下,我只需交换指针数组的字符串的基地址,就可以颠倒 dheeraj rahul (整体)字符串的位置. 通过它,指针似乎是指向字符串的指针类型,即指针指向整个字符串,而不是仅指向字符串的第一个字符. 因此,如果它们不是指向字符串的指针,而是仅指向char的指针;指向它们各自字符串的第一个字符,我们如何通过只交换指针数组中的 dheeraj rahul 的基地址来交换整个字符串.

解决方案

文字字符串实际上是一组只读字符(包括空终止符).与其他数组一样,它们会衰减到指向其第一个元素的指针.您的数组names是一个指针数组,每个指针都初始化为指向文字字符串的第一个元素(第一个字符)的指针.

交换names的元素时,仅交换指针.

通过使用"%p"格式说明符进行打印,您可以轻松地检查指针本身:

printf("names[2] = %p, names[3] = %p\n", (void *) names[2], (void *) names[3]);

如果在交换之前和之后执行此操作,则会看到指针已在数组中交换.文字字符串本身的内容仍将相同.

在指针交换之前,数组看起来像这样:

+----------+
| names[0] | -----> "pankaj"
+----------+
| names[1] | -----> "anshul"
+----------+
| names[2] | -----> "dheeraj"
+----------+
| names[3] | -----> "rahul"
+----------+

交换指针后,数组如下所示:

+----------+
| names[0] | -----> "pankaj"
+----------+
| names[1] | -----> "anshul"
+----------+
| names[2] | -\ /-> "dheeraj"
+----------+   x
| names[3] | -/ \-> "rahul"
+----------+

字符串本身的位置仍然相同.只是数组names的内容已更改.


作为一个小小的注解,由于C中的文字字符串是只读的,因此您应该真正将它们视为常量,并使用const char *指向它们.

int main(){
int i;
char *names[]={"pankaj",
               "anshul",
               "dheeraj",
               "rahul"
               };
for(i=0;i<4;i++)
    printf("\n%u",names[i]);}

When I coded this I found that all the strings are arranged in a contiguous manner. For ex. If names[0]=436, then names[1]=443, names[2]=450, names[3]=458, thus all strings have been allocated memory in a continuous fashion with \0 as a character in between. But strings could be allocated memory anywhere and their base addresses be stored in the array of pointers to these strings. Shouldn't be it like that? Moreover What is base type of these pointers in the array declared? Are they pointers to char Or pointers to strings.**like are these array of **char(*p)[6] type of pointers Or array of char *p type of pointers. We can handle the whole strings using this names array like:

int main()
{
char *names[]={"pankaj",
               "anshul",
               "dheeraj",
               "rahul"
               };
char *temp;
printf("Original:%s%s\n",names[2]names[3]);
temp=names[2];
names[2]=names[3];
names[3]=temp;
printf("New:%s%s",names[2],names[3]);
return 0;
}

In this I could able to reverse the positions of dheeraj and rahul (as a whole) string by just swapping the base addresses of the strings of the pointer array. Through it it looks like that the pointers are pointer to string type that is the pointers are pointing to the whole string instead of pointing to the first char of the string only. So if they aren't pointers to string and are pointers to char only; pointing to the first char of their respective strings , how are we able to swap the whole string by swapping only the base addresses of dheeraj and rahul in the pointer array.

解决方案

A literal string is really an array of read-only characters (including the null-terminator). As other arrays they decay to pointers to their first elements. You array names is an array of pointers, each pointer initialized as a pointer to the first element (first character) of the literal strings.

When you swap the elements of names, you only swap the pointers.

You could easily check the pointers themselves by printing using the "%p" format specifier:

printf("names[2] = %p, names[3] = %p\n", (void *) names[2], (void *) names[3]);

If you do that before and after the swap, you will see that the pointers have been swapped in your array. The contents of the literal strings themselves will still be the same.

The array look something like this before the pointer swap:

+----------+
| names[0] | -----> "pankaj"
+----------+
| names[1] | -----> "anshul"
+----------+
| names[2] | -----> "dheeraj"
+----------+
| names[3] | -----> "rahul"
+----------+

After the pointer swap the array looks like this:

+----------+
| names[0] | -----> "pankaj"
+----------+
| names[1] | -----> "anshul"
+----------+
| names[2] | -\ /-> "dheeraj"
+----------+   x
| names[3] | -/ \-> "rahul"
+----------+

The location of the strings themselves are still the same. It's only the contents of the array names that have changed.


As a small side-note, since literal strings in C are read-only you should really consider them as constant and use const char * for pointers to them.

这篇关于字符串的指针数组(指针的基本类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆