Thread: weird static char pointer array

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    6

    weird static char pointer array

    Hi,

    I am having a problem with a piece of code. Its a static pointer char array declared as follows.


    Code:
    static char *codagemc[2787] = { "urA", "xfs", "ypy", "unk", "xdw", "yoz", "pDA", "uls", "pBk", "eBA",
          ...... 2787 elements }
    The array is a pointer to characters. So when I print the array in gdb I get the following values

    Code:
    codagemc = 
    {0xd5d000 "urA", 0xd5d004 "xfs", 0xd5d008 "ypy", 0xd5d00c "unk", 0xd5d010 "xdw", 0xd5d014 "yoz", 0xd5d018 "pDA",   0xd5d01c "uls", 0xd5d020 "pBk", 0xd5d024 "eBA",
    ........  2787 elements}
    So its clear addresses are stored but I can't seem to make out the relationship between the addresses & the hard-coded characters.

    Intriguingly when I print out codagemc[0] I get 0xd5d000 "urA" . But when I print out *codagemc[0] I get 117 "u" (the first character of the string "urA" that is defined in static array. This particular property is true for all the values in the above pointer array). My point is dereferencing the address (oxd5d000 "urA") gives me the first character . I can't make out the relationship between the hex values, the string "urA" & the character "u".

    Also I have never seen addresses hard-coded in the array. Since the array is declared as static it should reside in the data/text segment of the program so hard-coding is fine I guess.

    The above array is used as a lookup table in a barcode generator. The idea is to map codewords (929 codewords * 3 modes = 2787) to alphabets. I would elaborate more if you desire so.

    I would appreciate any help on this problem. Thanks for your time.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Anator View Post
    So its clear addresses are stored but I can't seem to make out the relationship between the addresses & the hard-coded characters.

    Intriguingly when I print out codagemc[0] I get 0xd5d000 "urA" . But when I print out *codagemc[0] I get 117 "u" (the first character of the string "urA" that is defined in static array. This particular property is true for all the values in the above pointer array). My point is dereferencing the address (oxd5d000 "urA") gives me the first character .
    Of course it does, it should.

    I can't make out the relationship between the hex values, the string "urA" & the character "u". Also I have never seen addresses hard-coded in the array. Since the array is declared as static it should reside in the data/text segment of the program so hard-coding is fine I guess.
    They are not hard-coded. "Hardcoding" would be if you tried to provide the addressing yourself in the source. Which you should never do that.

    Anyway, your array is fine. It is good to be interested but don't get too distracted by very low level minor details like the address space of your array -- this is why you are working in C, the compiler and OS can take care of the addressing.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    char* is merely a pointer to a char. So naturally when we dereference it, we should get a char. Simple, right?
    The thing with strings is that it is usually an array. So we add one to the pointer until we find a character '\0'.
    The hex values are merely the address that the pointer points to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    6
    OK let me rephrase my question.

    I declare an array as follows.
    Code:
    static char *codagemc[1]  = { "urA" }
    What happens now? Does the compiler allocate consecutive space for 3 chars "u", "r" & "A" & then store the address of that first character in codagemc[0].

    When I print out the address stored in codagemc[0]. I get an address.

    Code:
    codagemc[0] = 0xd5d000
    So space for "urA" is stored in the data segement & the address of first char is stored in & codagemc[0]....... Did i get the whole flow right???

    Thanks,
    Anator

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Anator View Post
    OK let me rephrase my question.

    I declare an array as follows.
    Code:
    static char *codagemc[1]  = { "urA" }
    What happens now? Does the compiler allocate consecutive space for 3 chars "u", "r" & "A" & then store the address of that first character in codagemc[0].
    4 characters, not 3. There is a trailing null character at the end that is counted as far as allocation of space goes and yes, the address of the string is what's stored in codagemc[0]. Since we are dealing with a string literal here which should not be modified, the declaration for codagemc should actually be more like:
    Code:
    static const char *codagemc[1]  = { "urA" };


    When I print out the address stored in codagemc[0]. I get an address.

    Code:
    codagemc[0] = 0xd5d000
    So space for "urA" is stored in the data segement & the address of first char is stored in & codagemc[0]....... Did i get the whole flow right???

    Thanks,
    Anator
    Yes it's the address of the first character.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. typedef for pointer to array of char
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2003, 04:21 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM