Incompatible types problem in C / Variable Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pandaemonium
    New Member
    • Jun 2008
    • 2

    Incompatible types problem in C / Variable Problem

    I have two quick problems with the code I am writing:

    First, I define a struct of char arrays (strings) and then try accessing the same. I get an "incompatib le types in assignment" error:
    Code:
    struct {
         char name[50];
         char value[50];
    } varlist[20];
    
    int main(int argc, char *argv[]) {
         varlist[0].name = "foo";
         varlist[0].value = "bar";
    }
    .

    The second question I think has to do with a pointer issue, but I'm not sure.
    Code:
    char *mymethod(char str[]){
         char *result = malloc(100*sizeof(char));
         strcpy(result, atoi(global_variable));
         // global_variable is defined in a header file
         return result;
    }
    .
    When I run it, I get a "Passing arg 1 of 'atoi' makes pointer from interger without a cast" and I get the same error for arg 2 of strcpy.

    Please help, and thanks in advance.
  • jorba101
    New Member
    • Jun 2008
    • 86

    #2
    Originally posted by pandaemonium
    I have two quick problems with the code I am writing:

    First, I define a struct of char arrays (strings) and then try accessing the same. I get an "incompatib le types in assignment" error:
    Code:
    struct {
         char name[50];
         char value[50];
    } varlist[20];
    
    int main(int argc, char *argv[]) {
         varlist[0].name = "foo";
         varlist[0].value = "bar";
    }
    .
    It will not work. You're assigning the field "name" to point to a literal (namely "foo"). What you want to do is fill in the memory already pointed by name (as you have done when defining the var). You have to do strcpy. Something like...

    Code:
    strcpy( varlist[0].name, "foo");
    This will copy the string "foo" inside the memory pointed by varlist[0].name.

    The second question I think has to do with a pointer issue, but I'm not sure.
    Code:
    char *mymethod(char str[]){
         char *result = malloc(100*sizeof(char));
         strcpy(result, atoi(global_variable));
         // global_variable is defined in a header file
         return result;
    }
    .
    When I run it, I get a "Passing arg 1 of 'atoi' makes pointer from interger without a cast" and I get the same error for arg 2 of strcpy.

    Please help, and thanks in advance.
    I think you need function itoa (integer to ascii) instead of atoi (ascii to integer). It does not make sense to give an integer as a parameter to strcpy (this function is expecting a pointer to char -i.e. a string- in arg2).

    Comment

    • pandaemonium
      New Member
      • Jun 2008
      • 2

      #3
      Thank you... but what is the ISO standard way to convert ints to ASCII? According to man itoa it doesn't fall under standard C.

      Comment

      • jorba101
        New Member
        • Jun 2008
        • 86

        #4
        Woops, I did not know it was not standard.

        Then you can try:

        sprintf( yourTargetStrin g, "%d", yourIntVar );

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          Yeah, sprintf is about the only standard way to do int->ascii string. I've never understood why itoa isn't standard.

          Comment

          Working...