Confusion with the * before the variable name

0 john johnny · October 1, 2015
hi. can i get some clarification as to what's the use of * in this declaration. Why use * before the variable name?

char *word = "The quick brown fox"
char *loc = strrchr (word, 'o')


I notice that I can change it to.

char word[] = "The quick brown fox"
char *loc = strrchr (word, 'o')

but for some reason if I change to loc[] I receive an error.


Post a Reply

Replies

Oldest  Newest  Rating
0 c student · October 1, 2015
the * declares a pointer to whatever data type you have specified, for example,
int *num = NULL;

means that the variable named "num" is a pointer to an int.  what are the properties of num?  it is 4 bytes in size on 32-bit architecture or 8 bytes on 64-bit architecture (this is just a coincidence that the example i used with int has 4 bytes) to store the address of whatever it is equal to.

for a char, it is the same thing
char *c = NULL;

c is a pointer to a char type and is not a char itself.  same as before, it is 4 bytes in size on 32-bit architecture or 8 bytes on 64-bit architecture to store the address of what it is equal to.  you might be asking, if a char pointer is a pointer to a char data type, how come it can do with strings?  well, that's because a string is classified as any combination of characters (including none) which ends with a null terminator.  a char pointer can do with strings because it points to the address of the beginning of the string and then reads until it hits a null terminator.  like so:

c = "this is a string";

in this case, c is a pointer to the 't' character.  it is represented like this:
c -> [t][h][i][s][ ][i][s][ ][a][ ][s][t][r][i][n[g][\0]

as you can see, c points (the arrow) to a string stored in memory.  if you declare this string with a char pointer, it is stored in ROM (read-only memory) and cannot be modified.

so, if you want some sort of analogy, you can think of a pointer as your finger.  your finger is specified to point to a specific data type (although the data type only means the size of what you want to access, but let's not make this any more confusing) so to point to it, you will need to know the where (address) it is so you can point to it, otherwise, how can you point your finger at something if you dont know where it is?

arrays and pointers can seem to be synonymous but they are not.  an array stores information in elements, for example:
char string[] = "this is a string";

declares and sets a container of size string length + null terminator in RAM named "string" to store char data types.  it is represented like so:
[t][h][i][s][ ][i][s][ ][a][ ][s][t][r][i][n[g][\0]
^- the address here is the value of the "string" array

the difference between the array and the pointer is that the array "string" does not point to the beginning of the string like i have shown above.  in the case of the array, the array actually holds the address of the first char type it contains, that is, 't'.  so let me clarify, the pointer points to the address of the first char, an array is the address of the first character. here's a link that might explain a bit: http://c-faq.com/aryptr/aryptr2.html

so, now that we've clarified what pointers and arrays are, yes, your declarations of "word" is legitimate, however, your function strrchr returns a pointer to a char type, not an array.  man page found here: http://linux.die.net/man/3/strrchr

char *strrchr(const char *s, int c);

is a function which has a return type of a pointer to a char.  which is represented like so:
word: [T][h][e][ ][q][u][i][c][k][ ][b][r][o][w][n][ ][f][o][x][\0]
loc : ----------------------------------------------------^

loc points to the last occurrence of the character 'o' in the array "word"
  • 1

C

107,017 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator