What makes * so powerful here ?

This is a discussion on What makes * so powerful here ? within the C Programming forums, part of the General Programming Boards category; Hi Guys, I saw this example from a book that I was rather confused about : Code: const char *variable[4] ...

  1. #1
    Registered User Bracer's Avatar
    Join Date
    Oct 2009
    Location
    Singapore
    Posts
    12

    What makes * so powerful here ?

    Hi Guys,

    I saw this example from a book that I was rather confused about:

    Code:
    const char *variable[4] = { "One" , "Two" , "Three" , "Four" };
    ================================================
    I know this to be true:

    Code:
    char variable[12]="Hello World\n";
    so...shouldn't variable[4] have the ability to hold only four characters ?
    What makes the pointer "*" sign so powerful that it can change what supposedly a single character holder to a full modern form of a String type like those seen in Actionscript 3 or Visual Basic ?
    Last edited by Bracer; 10-06-2009 at 04:45 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    variable is an array of 4 pointers to const char. With the given initialisation, variable[0] points to the first character of "One", variable[1] points to the first character of "Two", etc.

    Quote Originally Posted by Bracer
    I know this to be true:
    You might want to note that "Hello World\n" has 13 characters (including the null character), and thus cannot fit into an array of 12 characters (if you actually want to include the null character, at least).
    Last edited by laserlight; 10-06-2009 at 04:42 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Bracer's Avatar
    Join Date
    Oct 2009
    Location
    Singapore
    Posts
    12
    Quote Originally Posted by laserlight
    variable is an array of 4 pointers to const char
    Thank for the prompt help
    4 pointers to char, ok.....but each char is just one character right ?
    Why is it that the minute the pointer points at a char data type it magically become this modern string instead of being what it is ? A single character holder ?

    P:S, 12 is correct, run this program
    Code:
    #import <stdio.h>
    #import <string.h>
    
    int main (int argc, const char *argv[] )
    {
    	char variable[12] = "Hello World\n";
    	printf ("The Length of the \"Hello World\" is %d .\n",strlen(variable));
    	return 0;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by Bracer
    4 pointers to char, ok.....but each char is just one character right ?
    Yes

    Quote Originally Posted by Bracer
    Why is it that the minute the pointer points at a char data type it magically become this modern string instead of being what it is ? A single character holder ?
    It is not so much magic as the standard convention that a string in C is a "contiguous sequence of characters terminated by and including the first null character". This means that given a pointer to the first char in a string, you can obtain all the chars of the string by pointing to the next character, and the next, etc, until you reach the null character.

    Quote Originally Posted by Bracer
    P:S, 12 is correct, run this program
    Your program prints the length of the string, but this program prints the number of characters required to store the string in an array:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        printf("The Length of the \"Hello World\" is %u .\n", sizeof("Hello World\n"));
        return 0;
    }
    By the way, use #include instead of the non-standard #import.

    Also, try this variant of your program:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char variable[12] = "Hello World\n";
        char x[] = "abc";
        printf("The Length of the \"Hello World\" is %d .\n", strlen(variable));
        return 0;
    }
    If you still get 12 as the length of the string, I'll change it such that you don't.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are 13 char's in "Hello World\n", *IF* you count the end of string char '\0' which the compiler will automatically place on a static string array (if space allows).

    BTW, you don't need to put in the number of char's if you declare a string array in this manner - just let the compiler count up the size it needs.

    (and strlen() doesn't count the end of string character).

    Code:
    char string[] = "Hello World\n";
    Will come out as a char array with space for 13 char's:
    "Hello World\n\0";

    fgets() is another string function that will add the end of string char to a string it puts into a char array - if it can.

  6. #6
    Registered User Bracer's Avatar
    Join Date
    Oct 2009
    Location
    Singapore
    Posts
    12
    My complier must be playing tricks on me as I am still getting 12 after running your program laserlight.
    But I understand the \0 [numeric 0 or alpha o...] logic now.
    Thanks to the both of you

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by Bracer
    My complier must be playing tricks on me as I am still getting 12 after running your program laserlight.
    It is a matter of where the arrays are placed in memory. I have to guess because this is implementation defined (and in fact I am assuming that the arrays will be adjacent).

    Quote Originally Posted by Bracer
    Thanks to the both of you
    You're welcome
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight View Post
    Yes
    How about "no" instead? It's a pointer to a character. When you point at an object, you can be pointing at any number of that type of object. Consider:
    Code:
    int x, y[4], *z;
    
    z = &x; /* point at an int, x */
    z = y; /* point at an array of four integers, y */
    Saying it's a "pointer to type foo" doesn't mean that it can only point to one single foo. Technically, the address it holds is that of a single type foo. So in that respect, yes, it's pointing at one. But, that doesn't mean it can't be part of a larger group of foo, that you can access through that pointer with pointer math.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by quzah
    z = y; /* point at an array of four integers, y */
    I admit that I often use the phrase "points to an array" as a shorthand, but it is wrong, strictly speaking. z does not point to an array of four integers, but z1 does:
    Code:
    int (*z1)[4] = &y;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, 'points to the first integer in an array of four' would have been wording it better. I wasn't trying to imply that I had a pointer-to-an-array.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    More importantly, points at the first element is more accurate. Cause:
    Code:
    int x, y[4], *z;
    z = &x; /* point at an int, x */
    z = y; /* point at an array of four integers, y */
    z = z + 1; /* points at the same array, put on 2nd element */
    So exactly where z points is good to keep in mind, rather than it points to an array.

  12. #12
    Registered User Bracer's Avatar
    Join Date
    Oct 2009
    Location
    Singapore
    Posts
    12
    I am trying to correlate my cheap understanding of C to Actionscript 3 so forgive me for my use of the word "magic", I am currently writing this as a note.

    My question is, now that string_array is a "magic string array", how do I retrieve its total count [The number of strings in the Array not the number of characters like strlen]? I know it's three in this case since I've declared it as such but is there a way to find this out in run time ? Do I now treat it as a normal C Array ?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char  string_variable[3] = "abc"; //A simple string declaration.
    
    	//By putting the asterick sign, you magically transform the char into
    	//not just a string...but an ARRAY of strings ! WOW !
    	
    	char* string_array[3] = {"Word One","Word Two","Word Three"};
    
    	printf("The word is \"%s\".\n",string_array[1]); //Output: The word is "Word Two".
    
    	return 0;
    }
    Last edited by Bracer; 10-06-2009 at 05:56 AM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'string_variable' isn't a string. It doesn't end in the '\0' character.

    What 'total count' do you mean? The sum of all of the lengths of all of its parts? Run through them a character at a time, or use a function that does the same thing, and add up all the values you get.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User Bracer's Avatar
    Join Date
    Oct 2009
    Location
    Singapore
    Posts
    12
    Oh no, I mean how many string objects are in the array, in this case I know there are three, but is there a .length() or .count() function I could use to retrieve it too ?

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In C, you have a string *ONLY* if there is an end of string char - '\0' - at the end of it. Anything else is just a bunch of char's - but *NOT* a string.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-10-2009, 10:29 AM
  2. Replies: 1
    Last Post: 12-23-2008, 08:39 AM
  3. Replies: 4
    Last Post: 12-23-2008, 12:23 AM
  4. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  5. doubleanti's gender poll...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 111
    Last Post: 08-04-2002, 12:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21