Thread: Query on accessing and assigning (unsigned char *) to int32_t

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Query on accessing and assigning (unsigned char *) to int32_t

    Hi All,

    I have a following code .

    Code:
          1 #include <stdio.h>
          2 #include <stdint.h>
          3
          4 struct temp
          5 {
          6     char sa_data[10];
          7
          8 };
          9
         10 int main()
         11 {
         12     unsigned char *c;
         13     struct temp test;
         14     static int32_t var;
         15
         16     c = &test.sa_data[0];
         17
         18     test.sa_data[0] = 0xAB;
         19     var = c[0];
         20
         21     printf("%x %x \n",c[0],var);
         22
         23     return 0;
         24 }
    Two queries here are.

    1. At line number 16, we are assigning address of sa_data[0] to unsigned char and while printing we are not using '*' , how it can still print the value of the variable instead address..?

    2. when i am trying to assign int32_t data type variable with unsigned char pointer ,why it does not throw any type cast warning in compilation?.

    Thanks in advance.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Not 100% sure on the 1st question, but it could be due to sa_data already being de-referenced? The character 'c' (in normal array syntax) will only point to the first element of the array. Arrays are passed to functions by reference and do not need the address being manually passed at compile time. It may also be down to the compiler being used or the difference between 16, 32 and 64bit integer manipulation. Again, I am not 100 percent on this.
    Double Helix STL

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. Because c[0] is not a pointer, it is an unsigned char.
    c is a pointer
    c[0] is a dereferenced pointer
    *c is the same dereferenced pointer exactly the same as c[0]

    2. The only thing going on here is the promotion of unsigned char (ie, the type of c[0]) to int32_t (the type of var).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    Thanks salem,

    One more query on subject int32_t . I have two machines with different gcc versions.

    When i include this data type in my program and try to compile i see that it depends on stdio.h in one and stdlib.h in another.

    Here with gcc version gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) , i see that its getting included from stdlib.h .

    Where as with gcc (SUSE Linux) 4.3.4 [gcc-4_3-branch revision 152973] , its getting included from stdio.h.

    By any chance do you know like , Is this observation correct?. I was testing by commenting the header files and seeing the compilation errors.

    Thanks in advance.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by vlrk View Post
    Here with gcc version gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) , i see that its getting included from stdlib.h .

    Where as with gcc (SUSE Linux) 4.3.4 [gcc-4_3-branch revision 152973] , its getting included from stdio.h.
    The printf function always comes from stdio.h. The typedef for int32_t comes from stdint.h. The standard says so. There is no room for ambiguity. You probably need to compile with -std=c99 or something similar, to get consistent behavior, as those typedefs were added in C99.

    On a side note, you need to upgrade. GCC will be releasing the 7.0 series in a few months. 4.3 is more than 7 years old, and that is an eternity, when it comes to compilers.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How covert unsigned char[4] to unsigned integer?
    By barracuda in forum C Programming
    Replies: 110
    Last Post: 02-23-2015, 04:00 AM
  2. Assigning int32_t to std::string, should this be allowed?
    By frogger in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2011, 10:22 PM
  3. Replies: 2
    Last Post: 10-06-2009, 09:37 AM
  4. Assigning negative number to unsigned variables.
    By sar_mahesh in forum C Programming
    Replies: 10
    Last Post: 09-27-2006, 05:47 PM
  5. char *, accessing/assigning problem
    By Extrovert in forum C++ Programming
    Replies: 4
    Last Post: 07-15-2005, 03:33 AM

Tags for this Thread