简体   繁体   中英

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. 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. Then here how p is acting like an array itself which is storing a string? 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.

Your first question:

Then here how p is acting like an array itself which is storing a string?

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.

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.

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 .

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

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. The difference between a character array and a string is the string is terminated with a special character '\\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". 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

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[1] is *(ptr + 1) which is a character at the 1st location of string 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. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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