Thread: Getting "incompatible types in assignment error."

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    117

    Getting "incompatible types in assignment error."

    I've posted this code before. Now I'm trying to take the names that I get out of this file I'm reading, and put them into a structure so I can have an array of my names retrieved.


    This part is the part giving me the same error for each line.
    Code:
           all[i].name = name;
           all[i].item = item;


    This is my code so far.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct names
    {
           char name[15];
           char item[99];
           int amount;
    };        
    
    int main(void)
    {
    
        char buffer[50];
        char name[15];
        char item[99];
        int amount;
        FILE *fp;
        FILE *fp2;
        int i=0;
    
        
        if ( (fp = fopen("names.csv", "r" )) == NULL )
        {
           printf("Couldn't open file\n");
           exit(1); 
        }
        
        while(fgets(buffer, sizeof(buffer), fp))
        {
           sscanf(buffer,"%[^,] ,%[^,], %d", &name, &item, &amount);
           strcat (name, ".txt");
           
           struct names all[100];
           all[i].name = name;
           all[i].item = item;
           all[i].amount = amount;
           i++;
    
           //create file for names
           if( (fp2 = fopen(name, "a")) == NULL )
           {
               printf("unable to open %s\n", name);
               exit( 1 );
           } 
           fprintf(fp2, "%s,%d\n", &item, amount);
           fclose(fp2);
    
          }
    fclose(fp);
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You cannot assign to an array like that. You must use strcpy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-26-2009, 10:07 AM
  2. "assignment from incompatible pointer type"
    By gkoenig in forum C Programming
    Replies: 1
    Last Post: 05-03-2008, 10:38 PM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. Error msg: "invalid lvalue in assignment"
    By mike00800 in forum C Programming
    Replies: 5
    Last Post: 11-07-2007, 08:38 AM
  5. Incompatible types in assignment error
    By Zildjian in forum C Programming
    Replies: 12
    Last Post: 10-03-2003, 01:15 PM