+ 3

Could anyone explain the link between strings and pointers?

I am really confused on how strings and pointers are linked together and why we don't have to use the indirection operator(*) for dereferencing as is the case for normal arrays. for example I am allowed to assign a value to string at the time of declaration but not afterward char s[5] = "John" ; RIGHT .. .. s= "John"; WRONG similarily I sometimes have to use the * operator for strings other times I don't. Ex: in the case of int arrays, we have to use the * operator if we want to print a value but not in the case of strings. Just printf("%s", s); will print out John. Why are we not using *s instead? Another trouble I am having is with storing multiple names in an array of char pointers. If I write *s[] = {"John", "Adams", "Charlie"....}; it works but if I want to input the names from the user, a statement like scanf("%s, s[0]); does not work. And, what's even more confusing now is that to print out a name, I have to use the * operator printf("%s, *(s+2)) ; or simply s[2] All this is very confusing to me as I don't really know when to use the * operator and when not to. It's all cuz the rules about pointers we learned in arrays do not hold true to strings. 1) We do not always use & 2) We do not always use * 3) We even are not allowed to assign using = operator Could anyone kindly clear all these doubts and explain what really is going inside the computer's memory when using pointers and strings. Also, could anyone suggest a resource that might help clear all these doubts and help solidify my concepts Your help is highly appreciated. Thanks:)

2nd Dec 2018, 6:02 AM
Yusha Arif
Yusha Arif - avatar
16 Respuestas
+ 2
Thanks a lot, everyone for your replies. I was watching some youtube videos on strings and pointers and as Asterisk pointed out, strings are actually pointers as well. One amusing thing I found was the way C deals with syntax ; char *s= "Hello"; basically creates a string literal "H E L L O" in the section of memory reserved only for constants. Hence if I try modifying Hello, the compiler would generate a syntax error. eg:- *(s+2) = 'b' ; ERROR However, interestingly enough, if I write a statement like this char s[] = "Hello", now what has ended up happening is that "H E L L O" has basically been stored in the read/write section of the RAM and hence is no longer a constant. Therefore, s[2] = 'g' ; is perfectly valid. Another interesting thing is that in the statement char s[]= "hello"; s is basically a POINTER. The character array itself is the string "hello", Thus, what can be concluded here is that the name of the array can indeed act as a pointer to that array. Now, as the %s format specifier requires a pointer that points to an array of character, AND... since s IS a pointer that points to an array of character, there is no need for * in the printf ( ) statement. However, in char *s[ ] = {"Adam", "John",....} ; s in not a pointer to an array of character, rather a pointer to an array of pointers. Hence, using the * operator, eg:- *(s+2), now what you have is a pointer pointing to an array of characters, exactly what the %s requires
2nd Dec 2018, 12:42 PM
Yusha Arif
Yusha Arif - avatar
+ 1
https://code.sololearn.com/ceJ9ps6VMv7G/?ref=app I've answered those questions with examples for you on this code. But i highly recommend you go check for yourself other ways to understand how C deal with strings if you're interested as well.
2nd Dec 2018, 8:21 AM
Zenobio
Zenobio - avatar
+ 1
Your question also carries good examples of how complicated pointers can be. I am Sure you are aware of how variables are declarated ( int foo = 2), so there is a variable with the value '2'. This variable and it's value need to be saved somewhere in your virtual memory(heap/Stack). Now every there are certain spaces in that memory, which can be allocated by those variables. Think of that Like a Matrix and let's say that 'int foo = 2' needs spaces of 4 or 8 bytes. On this memory-matrix the 4 or 8 fields are reserved for 'int foo', eventhough it doesn't need the whole 4 or 8 bytes ...
2nd Dec 2018, 9:53 AM
Pascal Pizzini
Pascal Pizzini - avatar
+ 1
For the Programm and system it is important to know which part of the memory belongs to which variable and so on. This is solved by using reference-adresses. So the space, 'int foo' is allowed to use has an adress, for examples '0xfffe4'. Pointers can actually use those Adresses to alter the variable on this space. This is named "call by reference" in contrast to most User "call by value"...
2nd Dec 2018, 9:58 AM
Pascal Pizzini
Pascal Pizzini - avatar
+ 1
In C there are No real 'string' datatypes, because a String is actually an Array of Characters. So you can define an array like: char foo [hey]; or char foo [ ] = "hey"; or even char *foo = "hey"; (These examples might Not be 100% correct, Syntax of declaration is nasty). Anyway, the later works because char *foo points to an "Array", you never definied, it simply Always Points to the First element (0) and will be able to Store the String as Long it is not bigger as size of char.
2nd Dec 2018, 10:06 AM
Pascal Pizzini
Pascal Pizzini - avatar
+ 1
The '&' is an Operator to interact with the memory adress like: scanf("%d" , &foo) which means: read the integer-value from I/O and save it to the adress of 'Foo'. Which will then overwrite the previous declaration. A Thing you can also do with pointers is to handle them in Arithmetics. You can increment/decrement them and all the other fun maths stuff.
2nd Dec 2018, 10:11 AM
Pascal Pizzini
Pascal Pizzini - avatar
+ 1
Using '&' with pointers will dereference them, if I recall it right. Oh and you can also declare a Pointer Array that holds pointers, the Pointer doing that needs to be a Double Pointer e.g: char **pointerarray [ ];
2nd Dec 2018, 10:14 AM
Pascal Pizzini
Pascal Pizzini - avatar
+ 1
Got a little dragged away tbh
2nd Dec 2018, 10:36 AM
Pascal Pizzini
Pascal Pizzini - avatar
+ 1
Yusha Arif 👍👍👍👍
2nd Dec 2018, 12:46 PM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 1
Thanks Asterisk.☺
2nd Dec 2018, 12:53 PM
Yusha Arif
Yusha Arif - avatar
0
this is an interesting question bro.... string is also a pointer thats why you can't use them interrelated but check this out... it should keep you up https://www.cs.bu.edu/teaching/c/string/intro/
2nd Dec 2018, 6:44 AM
✳AsterisK✳
✳AsterisK✳ - avatar
0
This explains it less confusing than me 😅: https://www.dyclassroom.com/c/c-pointers-and-strings
2nd Dec 2018, 10:19 AM
Pascal Pizzini
Pascal Pizzini - avatar
0
Pascal Pizzini your last post is cool... but your own explanation talk about what he do not understand than what the tutorial was discussing
2nd Dec 2018, 10:33 AM
✳AsterisK✳
✳AsterisK✳ - avatar
0
Thanks a ton @Julien Quentin. That explaination was really helpful. Could you kindly also explain the following behavior. char *s[4] ; scanf(%s, s[0] ) ; // ERROR Why is that?? Isnt s[0] pointing to an address?
2nd Dec 2018, 4:05 PM
Yusha Arif
Yusha Arif - avatar
0
string is a collection of characters ended with \0 while pointer is one of the derived data type through which we can store the address of another variable indirectly
3rd Dec 2018, 5:53 PM
Shubham Vijaykumar Patil
Shubham Vijaykumar Patil - avatar
0
Yusha Arif when dealing with strings input and output its better you use puts and gets in order to avoid errors
6th Dec 2018, 7:16 PM
✳AsterisK✳
✳AsterisK✳ - avatar