Thread: pointers, structures, and malloc

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    Switch to Bloodshed

    I switched compilers on a lark. I've gotten rid of my error messages. I have this code:
    Code:
      struct sdata_2 {
          char d21;
          char d22;
          char d23;
          char d24;
          char d2x[20];
    
         struct sdata_2 sdata2 = { 'a',  'b', 'c', 'd', 'Mike' }
    The compiler is very upset over Mike. It does not like multi-byte initial values. Since d2x is defined as a string of bytes, how do I get an initial value?

    TIA

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    try using a string rather than a multibyte character (which possibly would match an integer type, but certainly not a string):
    Code:
    struct sdata_2 sdata2 = { 'a',  'b', 'c', 'd', "Mike" }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    char is a single character, so 'use these'. char[20] is a bunch of characters, so "use these". The added advantage of the string is that it is properly null-terminated for you.

  4. #19
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I assume that the missing '}' in the above is just a copy and paste error, too.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by lugnut View Post
    The compiler is very upset over Mike. It does not like multi-byte initial values. Since d2x is defined as a string of bytes, how do I get an initial value?

    TIA
    The problem is that 'Mike' is an integer, aka int. 'Mike' is 'M' + 'i' + 'k' + 'e' (or some variant, not sure), which certainly does not fit into a char.
    Double quotes, on the other hand, is an array of chars, so it fits where single quotes does not.
    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.

  6. #21
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > The problem is that 'Mike' is an integer, aka int. 'Mike' is 'M' + 'i' + 'k' + 'e' (or some variant, not sure), which certainly does not fit into a char.
    Simpler than that. It's syntactically invalid

    > Double quotes, on the other hand, is an array of chars
    is a nul-terminated array of chars
    Very, Very, Very, Important

    Just remember, anything within single quotes must evalute to a single byte. Which of course has an integer value.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by zacs7 View Post
    > The problem is that 'Mike' is an integer, aka int. 'Mike' is 'M' + 'i' + 'k' + 'e' (or some variant, not sure), which certainly does not fit into a char.
    Simpler than that. It's syntactically invalid
    Are you sure?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int __cdecl main()
    {
    	unsigned int x = '1234';
    	cout << "0x" << hex << x << endl;
    }
    0x31323334
    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.

  8. #23
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    [zac@neux ~]$ cat ln.c 
    #include <stdio.h>
    
    int main(void)
    {
       unsigned int a = '1234';
       unsigned int b = '1' + '2' + '3' + '4';
    
       printf("&#37;X & %u\n", a, a);
       printf("%X & %u\n", b, b);
       return 0;
    }
    [zac@neux ~]$ gcc -Wall -pedantic -std=c89 ln.c -o ln
    ln.c:5:12: warning: multi-character character constant
    [zac@neux ~]$ ./ln 
    31323334 & 825373492
    CA & 202
    Turns out I was wrong :P. I wonder what it means then?
    Last edited by zacs7; 10-09-2008 at 04:13 PM. Reason: Where? :p robwhit is a liar!

  9. #24
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Undefined behavior!!!! &#37;X with an int! should be an unsigned int!! woop! *siren sounds*

    I'm gonna go do something else for a while.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zacs7 View Post
    Code:
    [zac@neux ~]$ cat ln.c 
    #include <stdio.h>
    
    int main(void)
    {
       unsigned int a = '1234';
       unsigned int b = '1' + '2' + '3' + '4';
    
       printf("%X & %u\n", a, a);
       printf("%X & %u\n", b, b);
       return 0;
    }
    [zac@neux ~]$ gcc -Wall -pedantic -std=c89 ln.c -o ln
    ln.c:5:12: warning: multi-character character constant
    [zac@neux ~]$ ./ln 
    31323334 & 825373492
    CA & 202
    Turns out I was wrong :P. I wonder what it means then?
    Conveniently, '1' is 0x31, '2' is 0x32, '3' is 0x33, and '4' is 0x34

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, structures, arrays and sudoku!!
    By AmbliKai in forum C Programming
    Replies: 13
    Last Post: 10-15-2008, 03:05 AM
  2. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  3. pointers and structures
    By coo_pal in forum C Programming
    Replies: 1
    Last Post: 07-23-2003, 04:45 AM
  4. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 03:26 PM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM