0

Question. How come the strings such as "C Programming" is compiled as address values as following?

 #include <stdio.h>

 void ShowAllString(int argc, char * argv[]) 
 {
     int i;
     for(i=0; i<argc; i++)
        printf("%s \n", argv[i]);
 }

 int main(void)
 {
     char * str[3]={
         "C Programming",
         "C++ Programming",
         "JAVA Programming"  };

    ShowAllString(3, str);
    return 0;
  }

My analogy was like the following... please correct me if you can.

char * argv[] if in parameter, that is equivalent to char ** argv . So the function is like void ShowAllString(int argc, char ** argv) to receive double pointer as argument. So it makes sense to have str as parameter because str is the name of the array char * str[3] and str is double pointer here as array name. char * str[3] is an array that is supposed to have three elements of pointers.... but how come such strings instead of address values are placed next to char * str[3]... this is where I am stuck!

Please help me!

  • I'm not sure exactly what you are asking? `char *` is a string. You can do `char * x = "asdf";` Your program is going to put the string "asdf" somewhere in memory, and return a pointer to the first character. – Porkbutts Mar 29 '13 at 22:09
  • @Porkbutts `char *` is a *type*. "String" is a representation of values. You can use `char *` to *point at* strings, but that type isn't synonymous with the term "string". – autistic Mar 29 '13 at 22:24

2 Answers2

1

When an array name is passed as parameter, it decays into a pointer. In your code, you have an array of 3 pointers. Therefore it decays into a pointer to a pointer.

jbgs
  • 2,795
  • 2
  • 21
  • 28
  • I wonder when the term *decays* was originally used to describe the conversion of an array expression to a pointer expression. It has such a negative connotation, as though the array has ceased to live and is breaking down... I don't like it. – autistic Mar 29 '13 at 22:32
  • @modifiablelvalue: "Decay" has been used to describe this process as long as I can remember. I don't know if this is the right place to discuss etymology, but "decay" is only correct term I know of for this process, I know of no synonyms. – Dietrich Epp Mar 29 '13 at 22:55
  • @DietrichEpp The C standard uses the word "convert", and by English definition "decay" just doesn't seem correct. An array, like all objects, might "decay" when it reaches the end of it's lifetime. – autistic Mar 29 '13 at 23:50
  • @modifiablelvalue: Yes, they are "converted", but "decay" is more specific here. It's not really appropriate to complain about common, accepted terminology -- the term "decay" might not tickle you the right way, but all your griping will not change the fact that it is the correct, specific term. Try doing a Google search for this, with and without using the word "decay" in your query, and you'll see what I mean. – Dietrich Epp Mar 30 '13 at 00:18
  • @DietrichEpp I used my dictionary to determine that the use of "decay" is incorrect in this case. I'm sure, if you ask an English professor (such as my partner), he/she will agree: Arrays do not *rot* to pointers. – autistic Mar 30 '13 at 02:50
  • ... and the standard explicitly uses the word "convert". It's been that way for as long as I can remember, and it was accepted by the forefathers of C, as well as many other well-known and respected figures, professors and the likes. – autistic Mar 30 '13 at 02:54
  • @modifiablelvalue: The dictionary is not an acceptable reference for technical definitions, an English professors are usually not experts on technical matters, and the C standard is not the only source for terminology. Do a Google search for "array decay", or see http://stackoverflow.com/questions/1461432/what-is-array-decaying -- search for "convert" in the answers to that question. – Dietrich Epp Mar 30 '13 at 05:21
  • @modifiablelvalue: It is also not appropriate to argue here in the comments: "decay" is already accepted terminology, this is not the appropriate forum to change that fact. – Dietrich Epp Mar 30 '13 at 05:23
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27218/discussion-between-modifiable-lvalue-and-dietrich-epp) – autistic Mar 30 '13 at 06:20
0

In C, char[] is a pointer to where an array of char are stored, one after another in memory. As it happens, this is a good way to store a string. In your code, you are creating an array of 3 pointers to arrays of char. The first of which, str[0], points to where the sequence of char "C Programming" is stored in memory, and so on.

(In everyday English one would generally call "a sequence of char" a "string", but technically, as the first comment correctly points out, this implies that char[] must by definition mean a string, which it doesn't.)

You pass a pointer to this array of pointers to the function, which then retrieves the pointers to each of the arrays of char ("strings"), which the printf line uses to get the strings (arrays of char) and print them.

Neil Townsend
  • 6,024
  • 5
  • 35
  • 52
  • `char[]` is a *type* (and an incomplete one, at that). "String" is a representation of values. You can (and *should*, when possible) use `char[]` to *store* strings, but that type isn't synonymous with the term "string". – autistic Mar 29 '13 at 22:26
  • @modifiablelvalue You are correct, I have tried to improve my answer to reflect your point of correction. – Neil Townsend Mar 29 '13 at 22:46