17
char *ptrChar;

I know that ptrChar is a pointer to char. What's the command for a pointer to string?

Edit: If it's the same (ptr to char vs. ptr to string) — what does the variable definition below represent?

char (*ptr)[N];

I'm confused.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user1980750
  • 693
  • 2
  • 8
  • 12
  • Related http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c and http://stackoverflow.com/questions/5595266/pointers-arrays-strings-and-malloc-in-c?rq=1 – user7116 Mar 01 '13 at 23:46
  • You're [looking for cdecl](http://cdecl.org/). – user7116 Mar 02 '13 at 00:03

3 Answers3

32

The same notation is used for pointing at a single character or the first character of a null-terminated string:

char c = 'Z';
char a[] = "Hello world";

char *ptr1 = &c;
char *ptr2 = a;      // Points to the 'H' of "Hello world"
char *ptr3 = &a[0];  // Also points to the 'H' of "Hello world"
char *ptr4 = &a[6];  // Points to the 'w' of "world"
char *ptr5 = a + 6;  // Also points to the 'w' of "world"

The values in ptr2 and ptr3 are the same; so are the values in ptr4 and ptr5. If you're going to treat some data as a string, it is important to make sure it is null terminated, and that you know how much space there is for you to use. Many problems are caused by not understanding what space is available and not knowing whether the string was properly null terminated.

Note that all the pointers above can be dereferenced as if they were an array:

 *ptr1    == 'Z'
  ptr1[0] == 'Z'

 *ptr2    == 'H'
  ptr2[0] == 'H'
  ptr2[4] == 'o'

 *ptr4    == 'w'
  ptr4[0] == 'w'
  ptr4[4] == 'd'

  ptr5[0] ==   ptr3[6]
*(ptr5+0) == *(ptr3+6)

Late addition to question

What does char (*ptr)[N]; represent?

This is a more complex beastie altogether. It is a pointer to an array of N characters. The type is quite different; the way it is used is quite different; the size of the object pointed to is quite different.

char (*ptr)[12] = &a;

(*ptr)[0] == 'H'
(*ptr)[6] == 'w'

*(*ptr + 6) == 'w'

Note that ptr + 1 points to undefined territory, but points 'one array of 12 bytes' beyond the start of a. Given a slightly different scenario:

char b[3][12] = { "Hello world", "Farewell", "Au revoir" };

char (*pb)[12] = &b[0];

Now:

(*(pb+0))[0] == 'H'
(*(pb+1))[0] == 'F'
(*(pb+2))[5] == 'v'

You probably won't come across pointers to arrays except by accident for quite some time; I've used them a few times in the last 25 years, but so few that I can count the occasions on the fingers of one hand (and several of those have been answering questions on Stack Overflow). Beyond knowing that they exist, that they are the result of taking the address of an array, and that you probably didn't want it, you don't really need to know more about pointers to arrays.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Hi @Jonathan Leffler, thanks for the detailed explanation! So formally this should be called "a pointer to a string"? – user1980750 Mar 02 '13 at 10:04
  • 1
    People often call a `char *` variable a pointer to a string; it means that the pointer points to a null-terminated array of characters. Not all `char *` variables are pointers to strings, though. In my example, `ptr1` is not a pointer to a string; it points to a single character, and it is not part of a null-terminated array of characters (which is what a string is). The pointer in `char (*pb)[12]` is absolutely not a pointer to a string, of course; it is a pointer to an array of 12 characters (and the array might or might not contain a null-terminated string). – Jonathan Leffler Mar 02 '13 at 14:30
  • Oh now i understood, thanks! It's all about strings being null-terminated in C. – user1980750 Mar 02 '13 at 14:39
  • ISO/IEC 9899:1999 §7.1.1 **Definitions of terms** _¶1 A string is a contiguous sequence of characters terminated by and including the first null character._ That's the first sentence of the section describing the C library. The C2011 standard says the same thing. – Jonathan Leffler Mar 02 '13 at 14:53
  • does int *a = {1,2,3} is valid or not ? – Suraj Jain Dec 27 '16 at 09:18
  • As it stands, `int *a = { 1, 2, 3 };` is invalid. If you wrote `int *a = (int[]){ 1, 2, 3};`, it should work. You've created a pointer to a compound literal. – Jonathan Leffler Dec 27 '16 at 16:21
11

The very same. A C string is nothing but an array of characters, so a pointer to a string is a pointer to an array of characters. And a pointer to an array is the very same as a pointer to its first element.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 2
    +1, C-style strings are by convention. They only exist when you follow it :) – user7116 Mar 01 '13 at 23:45
  • This is a common place for buffer overflows to happen. C does not bounds check the array, so if the programmer forgets to check user String input, they can write past the end of the array and put arbitrary (possibly malicious) data into memory beyond the array. – le3th4x0rbot Mar 01 '13 at 23:48
  • 10x for the answer. I'm confused. so what does the command above means: char (*ptr)[N];? – user1980750 Mar 01 '13 at 23:48
  • 1
    "And a pointer to an array is the very same as a pointer to its first element." No, the same address, but different types. – Daniel Fischer Mar 01 '13 at 23:48
1

The string is basically bounded from the place where it is pointed to (char *ptrChar;), to the null character (\0).

The char *ptrChar; actually points to the beginning of the string (char array), and thus that is the pointer to that string, so when you do like ptrChar[x] for example, you actually access the memory location x times after the beginning of the char (aka from where ptrChar is pointing to).

SmRndGuy
  • 1,719
  • 5
  • 30
  • 49