Thread: Incompatible types in assignment error

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Question Incompatible types in assignment error

    Hello,

    In the problem I'm working on now, I'm supposed to take a bunch of files, each containing a string per line, and merge them together so they form one sorted file. Each individual file is already sorted itself. Here is my code so far:

    Code:
     #include <stdio.h>
     #include <string.h>
    
     #define MAX_LINES 10000
    
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char sorted[MAX_LINES][BUFSIZ] = {0};
        char *tok;
        int stringNum;
        int i;
        int j;
        int k;
        FILE *fp;
    
        if (argc > 10) {
           fprintf(stderr, "%s: Too many command line arguments!\n",argv[0]);
           exit(1);
        }
    
        fp = fopen(argv[1],"r");
    
        while ( fgets(buf, BUFSIZ, fp) ) {
           strcpy(sorted[stringNum],buf);
           stringNum++;
        }
        
        for (i = 2; i < argc; i++) {
           fp = fopen(argv[i],"r");
           
           while ( fgets(buf, BUFSIZ, fp) ){
              for (j = 0; j < stringNum; j++) {
                 if (sorted[j] == NULL) strcpy(sorted[j],buf);
                 else if (strcmp(buf,sorted[j]) == 0) break;
                 else if (strcmp(buf,sorted[j]) < 0) {
                    for (k = stringNum; k >= j; k--) sorted[k+1] = sorted[k];
                    strcpy(sorted[j],buf);
                 }
                 else if (strcmp(buf,sorted[j]) > 0) continue;
              }
           }
        }
        for (i = 0; i < stringNum; i++) printf("%s", sorted[i]);
    
     }
    What I'm doing here is adding all the lines from the first file into a big array. Then, I want to take each file, compare every string in that file to what's already in the array, and then put it in where it's necessary. The bolded line is what's giving me a problem. There, if the word in buf is smaller than the one in the array, then I want to take every other word in the array and shift it up one spot to make room for the new word. However, when I compile it, I get an "incompatible types in assignment error". I can't figure this out. Also, any tips for making this program better would be appreciated, as I don't think this is the best way of doing it. Also, I'm supposed to compile it as follows: gcc -g -o Merge a4.c
    I'm not really clear on what this will do. Will it just let me run the program with the word Merge as opposed to using a.out? For more information on the problem go here: http://www.cs.dal.ca/~sedgwick/2132/a4/problem

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The "incompatible types in assignment error" is because you are trying to assign to an array. Different messages are follows.
    Code:
    [Error 64] Type mismatch (assignment) (char [512] = char *)
    Code:
    Lvalue required in function main
    Are you trying to copy strings?

    >Also, any tips for making this program better would be appreciated, as I don't think this is the best way of doing it.
    • #include <stdlib.h> for exit's prototype.
    • Check the return value of fopen. Should you continue if you failed to open a file?
    • Initialize stringNum.
    • Return a value from main.
    • The variable tok is not used.
    • LINES * BUFSIZ might be a bit too much space to expect on the stack.
    • Why not use a variable to store the return value of strcmp(buf,sorted[j]) instead of (potentially) recomparing the strings several times?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    On the bolded line, I'm trying to move all the entries in sorted up one spot in the array. Any suggestions on how to do that?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    To copy a string from one location to another, I'd recommend strcpy.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ok, here's what I have now.

    Code:
    #include <stdio.h>
     #include <string.h>
     #include <stdlib.h>
    
     #define MAX_LINES 1000
    
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char sorted[MAX_LINES][BUFSIZ] = {0};
        int stringNum = 0;
        int i;
        int j;
        int k;
        FILE *fp;
    
        if (argc > 10) {
           fprintf(stderr, "%s: Too many command line arguments!\n",argv[0]);
           exit(1);
        }
    
        fp = fopen(argv[1],"r");
    
        while ( fgets(buf, BUFSIZ, fp) ) {
           strcpy(sorted[stringNum],buf);
           stringNum++;
        }
        
        if (argc > 2) {
           for (i = 2; i < argc; i++) {
              fp = fopen(argv[i],"r");
           
              while ( fgets(buf, BUFSIZ, fp) ){
                 for (j = 0; j < stringNum; j++) {
                    if (sorted[j] == NULL) {
                       strcpy(sorted[j],buf);
                       stringNum++;
                    }
                    else if (strcmp(buf,sorted[j]) == 0) break;
                    else if (strcmp(buf,sorted[j]) < 0) {
                       for (k = stringNum; k >= j; k--) strcpy(sorted[k+1],sorted[k]);
                       strcpy(sorted[j],buf);
                       stringNum++;
                    }
                    else if (strcmp(buf,sorted[j]) > 0) continue;
                 }
              }
           }
        }
        
        for (i = 0; i < stringNum; i++) printf("%s", sorted[i]);
    
     }
    This compiles with no errors and works for one file. However, when I try to merge more than one file, it produces a Segmentation Fault (I really hate those things). Any suggestions for what might be going wrong? I have a feeling it has to do with the moving of strings in the sorted array when buf is smaller than the element in sorted.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >I have a feeling it has to do with the moving of strings in the sorted array when buf is smaller than the element in sorted.

    After moving elements up in the array, you might want to break out of the for ( j = 0; j < stringNum; j++ ) loop.

    I haven't taken a terribly detailed look, but there may be boundary condition issues.
    Code:
    for (k = stringNum; k >= j; k--) strcpy(sorted[k+1],sorted[k]);
    This looks like a candidate for further review.

    And my little test with two files sorted the first and concatenated the second unsorted.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Alright, I'm really close to getting this working right. However, a problem I'm having now is when I merge the files, it doesn't add strings to the output file if it's bigger than all the strings in it. Say after the first file is put into the output file and the last word starts with H, if the next file(s) contains a string with a letter that comes after H, it doesn't get added. Here is my code now:

    Code:
     #include <stdio.h>
     #include <string.h>
     #include <stdlib.h>
    
     #define MAX_LINES 1000
    
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char sorted[MAX_LINES][BUFSIZ] = {0};
        int stringNum = 0;
        int i;
        int j;
        int k;
        FILE *fp;
    
        if (argc > 10) {
           fprintf(stderr, "%s: Too many command line arguments!\n",argv[0]);
           exit(1);
        }
    
        fp = fopen(argv[1],"r");
    
        while ( fgets(buf, BUFSIZ, fp) ) {
           strcpy(sorted[stringNum],buf);
           stringNum++;
        }
        
        if (argc > 2) {
           for (i = 2; i < argc; i++) {
              fp = fopen(argv[i],"r");
           
              while ( fgets(buf, BUFSIZ, fp) ){
                 for (j = 0; j < stringNum; j++) {
                    if (sorted[j] == NULL) {
                       strcpy(sorted[j],buf);
                       stringNum++;
                       break;
                    }
                    else if (strcmp(buf,sorted[j]) > 0) continue;
                    else if (strcmp(buf,sorted[j]) == 0) break;
                    else if (strcmp(buf,sorted[j]) < 0) {
                       for (k = stringNum-1; k >= j; k--) strcpy(sorted[k+1],sorted[k]);
                       strcpy(sorted[j],buf);
                       stringNum++;
                       break;
                    }
                 }
              }
           }
        }
        
        for (i = 0; i < stringNum; i++) printf("%s", sorted[i]);
    
     }

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Anyone have any suggestions as to what's wrong?

  9. #9
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Zildjian
    Alright, I'm really close to getting this working right. However, a problem I'm having now is when I merge the files, it doesn't add strings to the output file if it's bigger than all the strings in it....
    Code:
     #include <stdio.h>
     #include <string.h>
     #include <stdlib.h>
    
     #define MAX_LINES 1000
    
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char sorted[MAX_LINES][BUFSIZ] = {0};
        int stringNum = 0;
        int i;
        int j;
        int k;
        FILE *fp;
    
        if (argc > 10) {
           fprintf(stderr, "%s: Too many command line arguments!\n",argv[0]);
           exit(1);
        }
    
        fp = fopen(argv[1],"r");
    
        while ( fgets(buf, BUFSIZ, fp) ) {
           strcpy(sorted[stringNum],buf);
           stringNum++;
        }
        
        if (argc > 2) {
           for (i = 2; i < argc; i++) {
              fp = fopen(argv[i ],"r");
           
              while ( fgets(buf, BUFSIZ, fp) ){
                 for (j = 0; j < stringNum; j++) {
                    // This is your problem area -- remove
                    if (sorted[j] == NULL) {
                       strcpy(sorted[j],buf);
                       stringNum++;
                       break;
                    } 
                     // this if not needed 
                    else if (strcmp(buf,sorted[j]) > 0) continue;
                    else if (strcmp(buf,sorted[j]) == 0) break;
                    else if (strcmp(buf,sorted[j]) < 0) {
                       for (k = stringNum-1; k >= j; k--) 
                           strcpy(sorted[k+1],sorted[k]);
                       strcpy(sorted[j],buf);
                       stringNum++;
                       break;
                    }
                 }
                 // add... 
                 if (j >= stringNum)
                 {
                     strcpy(sorted[[stringNum],buf);
                     stringNum++;
                 }
    
              }
           }
        }
        
        for (i = 0; i < stringNum; i++) printf("%s", sorted[ i]);
    
     }
    Instead of checking for NULL, let the loop end. Then check if you went through the entire loop if (j >= stringNum).

    Based on how you are loading sorted, you will never get an empty entry, unless there are blank lines in the files, in which case you'd have other problems.

    Also, as suggested earlier, test for an error after fopen
    Last edited by WaltP; 10-02-2003 at 03:14 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  10. #10
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ok, great! I'm almost there, I think. I have it sorting files correctly. However, what I need to do is if one of the command line arguments is "-" I'm supposed to use stdin for that file. Here is my code now:

    Code:
     #include <stdio.h>
     #include <string.h>
     #include <stdlib.h>
    
     #define MAX_LINES 1000
    
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char sorted[MAX_LINES][BUFSIZ] = {0};
        int stringNum = 0;
        int i;
        int j;
        int k;
        FILE *fp;
    
        if (argc > 11) {
           fprintf(stderr, "%s: Too many command line arguments!\n",argv[0]);
           exit(1);
        }
    
        if (argv[1] != "-") fp = fopen(argv[1],"r");
        else fp = stdin;
        
        while ( fgets(buf, BUFSIZ, fp) ) {
           strcpy(sorted[stringNum],buf);
           stringNum++;
        }
        
        if (argc > 2) {
           for (i = 2; i < argc; i++) {
               if (argv[i] != "-") fp = fopen(argv[i],"r");
               else fp = stdin;
           
               while ( fgets(buf, BUFSIZ, fp) ){
                  for (j = 0; j < stringNum; j++) {
                     if (strcmp(buf,sorted[j]) == 0) break;
                     else if (strcmp(buf,sorted[j]) < 0) {
                        for (k = stringNum-1; k >= j; k--) strcpy(sorted[k+1],sorted[k]);
                        strcpy(sorted[j],buf);
                        stringNum++;
                        break;
                     }
                  }
                  if (j >= stringNum) {
                     strcpy(sorted[stringNum],buf);
                     stringNum++;
                  }
               }
           }
        }
        
        for (i = 0; i < stringNum; i++) printf("%s", sorted[i]);
    
     }
    I'm not sure if I'm doing this correctly. The bolded lines are where I'm trying to use stdin. The program compiles correctly this way. However, when I try to run it as follows:
    ./Merge f1 f2 | ./Merge - f3 f4 >f1to4
    I get a Segmentation Fault. Any suggestions on how to use stdin when the file name is "-"?

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Post Reading from stdin problem

    Hi,

    I posted this in another thread, but the subject of the thread is completely different from what's going on in it now, so I figured a new thread would be better. Anyway, here's my message from that thread:

    Ok, great! I'm almost there, I think. I have it sorting files correctly. However, what I need to do is if one of the command line arguments is "-" I'm supposed to use stdin for that file. Here is my code now:

    Code:
    #include <stdio.h>
     #include <string.h>
     #include <stdlib.h>
    
     #define MAX_LINES 1000
    
     main(int argc, char *argv[]) {
        char buf[BUFSIZ];
        char sorted[MAX_LINES][BUFSIZ] = {0};
        int stringNum = 0;
        int i;
        int j;
        int k;
        FILE *fp;
    
        if (argc > 11) {
           fprintf(stderr, "%s: Too many command line arguments!\n",argv[0]);
           exit(1);
        }
    
        if (argv[1] != "-") fp = fopen(argv[1],"r");
        else fp = stdin; 
        
        while ( fgets(buf, BUFSIZ, fp) ) {
           strcpy(sorted[stringNum],buf);
           stringNum++;
        }
        
        if (argc > 2) {
           for (i = 2; i < argc; i++) {
               if (argv[i] != "-") fp = fopen(argv[i],"r");
               else fp = stdin;
           
               while ( fgets(buf, BUFSIZ, fp) ){
                  for (j = 0; j < stringNum; j++) {
                     if (strcmp(buf,sorted[j]) == 0) break;
                     else if (strcmp(buf,sorted[j]) < 0) {
                        for (k = stringNum-1; k >= j; k--) strcpy(sorted[k+1],sorted[k]);
                        strcpy(sorted[j],buf);
                        stringNum++;
                        break;
                     }
                  }
                  if (j >= stringNum) {
                     strcpy(sorted[stringNum],buf);
                     stringNum++;
                  }
               }
           }
        }
        
        for (i = 0; i < stringNum; i++) printf("%s", sorted[i]);
    
     }
    I'm not sure if I'm doing this correctly. The bolded lines are where I'm trying to use stdin. The program compiles correctly this way. However, when I try to run it as follows:
    ./Merge f1 f2 | ./Merge - f3 f4 >f1to4
    I get a Segmentation Fault. What I'm trying to do there is merge f1 and f2, then "pipe" the results into the second Merge where it is the first file and it gets merged with f3 and f4. Any suggestions on how to do this right? I'm stumped right now.

    For more information about the problem, please see the following thread: http://cboard.cprogramming.com/showt...threadid=45445
    Last edited by Zildjian; 10-02-2003 at 07:15 PM.

  12. #12
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    Code:
        if (argv[1] != "-") fp = fopen(argv[1],"r");
        else fp = stdin;

    use strcmp()

    Code:
    if(!strcmp("hi","hi")); //correct
    if("hi"=="hi") //incorrect
    so it would be like:

    Code:
    if(strcmp(argv[1],"-") 
      fp = fopen(argv[1],"r");
    else
      fp = stdin;
    -LC
    Last edited by Lynux-Penguin; 10-02-2003 at 09:16 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > argv[1] != "-"
    This isn't a string comparison

    This is
    if ( strcmp(argv[1],"-") == 0



    PS
    Threads containing the same subject merged
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM