简体   繁体   English

C中的字符指针

[英]Character Pointer in C

Experts I've some doubts in this programme. 专家我对此计划有些怀疑。

#include<stdio.h>
void main() {
  char str1[] = "Hello";
  char * p = "Hello", * s, * q;
  p = "Bye";
  printf("%s", p);
  s = p;
  printf("%s", s);
  q = str1;
  printf("\n%s", q);
}

Here p,s and q are character pointers. 这里的p,s和q是字符指针。 As we know pointers store the address of a variable and suppose if we used p=str1 then it should store the base address of array str1. 众所周知,指针存储变量的地址,并假设如果我们使用p = str1,则它应该存储数组str1的基地址。 Then here how p is acting like an array itself which is storing a string? 那么在这里p如何像存储字符串的数组一样工作? I've not really understood what's character pointer. 我还不太了解什么是字符指针。 Is it not really a pointer? 真的不是指针吗? Because we are not using & and neither are we getting the address as an output. 因为我们没有使用&,我们也没有获得地址作为输出。 Please help. 请帮忙。 Thank you. 谢谢。

You are very much correct here: 您在这里非常正确:

As we know pointers store the address of a variable and suppose if we used p=str1 then it should store the base address of array str1. 众所周知,指针存储变量的地址,并假设如果我们使用p = str1,则它应该存储数组str1的基地址。

Your first question: 您的第一个问题:

Then here how p is acting like an array itself which is storing a string? 那么在这里p如何像存储字符串的数组一样工作?

You can say p is acting like an array but in reality, it is just a pointer which is pointing to the base address of string str1 which is a null-terminated string. 您可以说p的行为就像一个数组,但实际上,它只是一个指针,它指向字符串str1的基地址,该字符串是一个以空字符结尾的字符串。

Your second question: 您的第二个问题:

I've not really understood what's character pointer. 我还不太了解什么是字符指针。 Is it not really a pointer? 真的不是指针吗?

A character pointer is again a pointer like the pointers to other types in C. 字符指针还是类似于C中其他类型的指针的指针。

But there is catch here. 但是这里有个陷阱。 when you do: 当您这样做时:

char a = 'A';
char *ptr = &a; // ptr points to character 'A'

Here ptr is pointer to a character . 这里的ptr指向字符的指针

But when you do: 但是当您这样做时:

char *str = "Hello";
char *ptr = str;  // ptr points to first character of string str

Here ptr is pointer to a string 这里ptr指向字符串的指针

A point to note here is - pointer to a character is different from the pointer to a string. 这里要注意的一点是-指向字符的指针与指向字符串的指针不同。

In C, strings are defined as an array of characters. 在C语言中,字符串定义为字符数组。 The difference between a character array and a string is the string is terminated with a special character '\\0'. 字符数组和字符串之间的区别是字符串以特殊字符'\\ 0'终止。 So, 所以,

char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char str[] = "Hello";

Both are same but there is a difference in the way they have been initialized. 两者相同,但是初始化方式有所不同。

Wherever in the program, we use str , this will give the base address of string "Hello". 无论在程序中的何处,我们都使用str ,这将给出字符串“ Hello”的基地址。 Similarly, string literals are also an array of characters with an exception that they can not be changed because the compiler may put them read-only data section. 同样,字符串文字也是字符数组,但不能更改,因为编译器可能会将它们放在只读数据段中。

So, if we have char *ptr = str then 所以,如果我们有char *ptr = str

ptr[0] == str[0]
ptr[1] == str[1]
.....
..... and so on

Because, 因为,

ptr[0] is *(ptr + 0) which is character at 0th location of string str and can also be written as *ptr and ptr[0]*(ptr + 0) ,它是字符串str第0个位置的字符,也可以写为*ptr

ptr[1] is *(ptr + 1) which is a character at the 1st location of string str . ptr[1]*(ptr + 1) ,它是字符串str的第一位置的字符。

When we increment a pointer, it gets incremented in steps of the object size that the pointer points to. 当我们增加一个指针时,它会以该指针指向的对象大小的步长递增。 Here, ptr is pointer to char so, ptr+1 will give address of next character and *(ptr + 1) give the character at that location. 在这里,ptr是指向char指针,因此, ptr+1将给出下一个字符的地址,而*(ptr + 1)给出该位置的字符。 That's why to the user it looks like its acting like an array. 这就是为什么对于用户而言,它看起来像一个数组。

Your third question: 您的第三个问题:

Because we are not using & and neither are we getting the address as an output. 因为我们没有使用&,我们也没有获得地址作为输出。

If I am getting it correctly you want to print the base address of the string, the pointer is pointing to. 如果我正确获取它,则要打印字符串的基地址,指针指向该地址。

In order to get the base address of string a pointer is pointing to you need to use %p . 为了获得字符串的基地址,指针指向您需要使用%p Like this: 像这样:

#include<stdio.h>

int main() {
  char str1[] = "Hello";
  char * ptr = str1;
  printf ("%s\n", str1);
  printf ("%s\n", ptr);
  printf ("%p\n", ptr);
  printf ("%p\n", str1);
  printf ("%p\n", &str1[0]);
  return 0;
}

Output on my system: 我的系统上的输出:

Hello
Hello
0x7fff5e997b46
0x7fff5e997b46
0x7fff5e997b46

Here you can see - ptr , str and &str[0] giving same address. 在这里,您可以看到ptrstr&str[0]给出了相同的地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM