Could someone clarify this one confusion I have with C Strings, arrays versus pointers?

You're right, a pointer can only point to one memory location. When dealing with arrays, the pointer points at the location of the first element. When you use printf it basically takes the pointer (pointing to the first element of the string), and prints until reaching the null terminating character 0 .

Up vote 0 down vote favorite share g+ share fb share tw.

Just a quick question. I can write char string = " char *p = string; printf("%s", p); And have it print But how is the pointer working here? Each point in an array has a separate memory location, right?

So the string array being 12 long, would take up 12 memory spaces. I thought a pointer could only point to one memory location, not 12. How is the pointer p achieving this?

With normal arrays and pointers if you want to scale the array you do *p++, as you're going through each memory location and printing its value. Why is that you have to traverse the array 1 by 1 there, but here it simply points to the whole thing? It just seems to me like with one (int arrays) you're incrementing the pointers as each pointer can only point to one memory location, but with char arrays it can point to all of them somehow.

C arrays string char link|improve this question asked 17 hours agoDoug Smith1347 100% accept rate.

You're right, a pointer can only point to one memory location. When dealing with arrays, the pointer points at the location of the first element. When you use printf, it basically takes the pointer (pointing to the first element of the string), and prints until reaching the null terminating character, \0.

Here is a good explanation of pointers vs arrays in c: cs.bu.edu/teaching/cpp/string/array-vs-ptr.

Agree. And the loop is failing because p is being incremented twice. Adding just 1, 3 and 6.

– danh 17 hours ago Oh. Thanks greatly, can't get any simpler than that. And I assume that while the above example is a rather useless example of using a pointer to print a string, there are practical uses to it as well, like normal pointers with integers and such?

– Doug Smith 17 hours ago And thanks @danh, I saw that as soon as I posted it, haha. Whoops. – Doug Smith 17 hours ago @DougSmith - I'm not exactly sure what your question is.

You cannot print an entire array of integers in one shot. In that sense, strings are special. – prelic 17 hours ago.

I thought a pointer could only point to one memory location, not 12. How is the pointer p achieving this? P is a pointer to char and not a pointer to an array.

It points to the first element of the string array. Now the value of an array is a pointer to its first element so this declaration: char *p = string; is actually equivalent to: char *p = &string0; If you increment the pointer: p++; p will point to the next element of the array, that is to string1.

I see, I see! Thanks greatly. – Doug Smith 17 hours ago.

The pointer to an array is really a pointer to the first address... And printf will scan from that address on untill it finds the null char... %c and %s differ on that behavior.

You use %s so it prints till it '\0'. Why is that you have to traverse the array 1 by 1 there, but here it simply points to the whole thing? Here, you use a pointer to a char, and you wanted to print the string.

So it's fine. Suppose, if you are using an pointer to an int or other types, this will not quite work. So pointer arithmetic like p++ is used.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions