Thread: Assigning something

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    29

    Assigning something

    Im trying to initialize code with keyCode and id with keyId respectively. Why do I get ['strcpy' makes integer from pointer without cast]?


    Code:
    Key * ky_new(const char * keyCode, int keyId)
    {
     Key * ky = (Key *)malloc(sizeof(Key));
     if (ky != NULL)
        {
         strcpy(ky->code , keyCode); 
         ky->id = keyId;
        }
     return ky;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because you don't have #include <string.h> at the top of the file.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    29
    Nope, I've double checked. I do have #include <string.h> .What's the problem then?Thkc

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Not clear can u explain it further and one more thing r u getting an error or result is not correct if result is not correct then u can do like

    memset(ky, 0, sizeof(ky));

    or always do allocation with calloc

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Jasper View Post
    Nope, I've double checked. I do have #include <string.h> .What's the problem then?Thkc
    If you had string.h at the top of this file (not just somewhere in your code), then you wouldn't be getting this error (you might be "making pointer from integer without a cast" but not the other way around).

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    29
    This has to do with modular decomposition. The code I gave was in the c file. This one below is in the h.file.

    Code:
    typedef struct 
    {
     int id;
     char code;
    }Key;

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So now you are making a pointer from an integer without a cast, because code is a single character and cannot be used in a string context.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    code is a char; it can only hold one character.
    strcpy copies in a string, but you do not have any place to copy that string...
    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.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    29
    So.... should I change strcpy(ky->code , keyCode); to ky->code = keyCode; ? Ive tried it but it still gives the same exact error. Thkx

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to change your struct.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to make sure there is room in your code member. A string in C is an array of characters.
    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.

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by tabstop View Post
    You need to change your struct.

    or change the const char * keyCode to const char keyCode

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Neither. You do not seem to understand what they do.
    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.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    29
    Elysia, u mean Im supposed to allocate memory for code? If that is so,is the code below the way to do it?
    Code:
     keyCode = (char *) malloc(code * sizeof(char);

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    keyCode already contains valid memory!
    However, ky->code does not! It contains room for ONE char only. Either you expand it using an array or make it a pointer and allocate using malloc. Only then you have to remember to free it later.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning Ability Scores
    By SteelySam in forum C++ Programming
    Replies: 1
    Last Post: 07-10-2008, 02:16 PM
  2. Assigning a non null-terminated string
    By BattlePanic in forum C Programming
    Replies: 7
    Last Post: 05-04-2008, 10:02 PM
  3. silence warning when assigning pointers
    By eth0 in forum C Programming
    Replies: 5
    Last Post: 10-27-2005, 11:18 AM
  4. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 05:53 PM
  5. Assigning the value of a const array to a normal array
    By Accident Prone in forum C++ Programming
    Replies: 6
    Last Post: 08-11-2003, 10:40 PM