What would be a declaration likechar *song;
What does the * does? Is it an array, a pointer or something else?
Asked
Active
Viewed 6.4k times
9

Bernardo Meurer
- 2,295
- 5
- 31
- 52
-
If i had one i would guess its a pointer, but i'm not sure, thats why i'm asking – Bernardo Meurer Jul 19 '13 at 15:27
-
1Yes, it is indeed a pointer, but here is something you should read: [1](http://webcache.googleusercontent.com/search?q=cache:ZeMBWj5-UpIJ:www.iu.hio.no/~mark/CTutorial/CTutorial.html+&cd=1&hl=hu&ct=clnk&gl=hu) (but insert `int main()` because it's ancient), [2](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) – Jul 19 '13 at 15:29
2 Answers
8
The * (Asterisk) indicates the variable is a pointer. As for a small example:
int x = 0;
int *y = &x; //y is pointing to x
const char* myText = "Text";
You might however be interested in learning a bit more about what pointers are.

Floris Velleman
- 4,848
- 4
- 29
- 46
-
1Thanks, i got a little confused because i have a huge declaration, like this `char *song = "smb:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6"` So i didn't know if it was an array or what – Bernardo Meurer Jul 19 '13 at 15:31
-
2@BernardoMeurerCosta The string literal is an array itself, but it can decay into a pointer to its first element. – Jul 19 '13 at 15:33
4
H2CO3 is right, you should read up on c, and pointers.
char *song = "smb:d=4,o=5,b=......."
Is the does the same thing as the code below
char song[] = "smb:d=4,o=5,b=......."
In both cases song is a pointer to an array of strings. C++ has a string object, but plain C used c_strings. A c_string is simply a char array. You have what looks like a c_string.
*song //the same as "song[0]" will equal 's'
*(song+1) //the same as "song[1]" will equal 'm'
*(song+2) //the same as "song[2]" will equal 'b'
and so on

John b
- 1,338
- 8
- 16