Thread: incompatible types error

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    14

    incompatible types error

    Hi everyone,
    Can someone please explain my mistake. The code is supposed to convert characters from an array into their respective ascii integers, and append a 0 if the number is less than 3 digits long. It then supposed to put it all together into one string. Many thanks.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void){
    
    char file[] = "This is a test";
    char *ptr = file;
    int length = strlen(file);
    int i, numbers[length];
    
    
    for (i =0; i <= length; i++){
    numbers[i] = *ptr;
    printf("%i\n", numbers[i]);
    ptr++;
    }
    
    //////Problem is here//////////
    char stringout[100000000] = "";
    char temp[4];
    
    for (i =0; i <= length; i++){
    sprintf(temp, "%i", numbers[i]);
        if (strlen(temp) == 2){
        temp = strcat("0", temp);
                  }
    stringout = strcat(stringout, temp);        
    }              
    ///////////////////////////////
    
    return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can't assign the value of an array; that is, since stringout is an array, you can never do "stringout = ". You can only modify the contents of the array, which strcat is perfectly happy to do.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Your first error:

    Code:
       temp = strcat("0", temp);
    The first argument to strcat must be a writeable destination string. "0" is a literal string, and isn't writable. If you take away the assignment as said above it'll probably compile but crash when you run it.

    There are a few ways around this -- you can memmove temp to temp+1 (shift all the chars right by 1) then write '0' into the first element.
    Or, you could use sprintf:
    Code:
       sprintf(temp2, "0%s", temp);
    Note I've used a different variable for the destination -- you can't guarantee that temp will be read before 0 is written to temp2. So if you use the same temp, you could end up with garbage results.

    One more thing that took me a minute to figure out - if your program crashes as soon as you start it, before doing anything, it might be because of this:
    Code:
    char stringout[100000000] = "";
    That's around about 100MB, which many systems may not let you allocate on the stack. In this case you know the length of the string and that every character will be 3 characters, plus a NULL at the end. So you can work out how much space you need and malloc it.



    [edited - thought of an easier way]

    If you just want all the numbers to be 3 digits long, zero padded, you can change your first sprintf to:
    Code:
        sprintf(temp, "%03i", numbers[i]);
    That'd also turn a tab character into 009 rather than 09, which might or might not be something you care about.
    Last edited by smokeyangel; 11-22-2013 at 06:17 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > for (i =0; i <= length; i++)
    In addition, both these loops fall off the end of your array.
    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.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Code:
    char stringout[100000000] = "";
    That's a lot of memory and it could lead to unexpected results if your OS/C Implementation/CPU/whatever uses a stack for local variables. Even dismissing this potential problem, is a 95MB array really a good idea?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C: Errors for Incompatible Types
    By ekay1 in forum C Programming
    Replies: 5
    Last Post: 10-23-2013, 08:03 PM
  2. Getting "incompatible types in assignment error."
    By mgracecar in forum C Programming
    Replies: 1
    Last Post: 02-29-2012, 06:38 PM
  3. Replies: 2
    Last Post: 12-26-2009, 10:07 AM
  4. Incompatible types in assignment error
    By Zildjian in forum C Programming
    Replies: 12
    Last Post: 10-03-2003, 01:15 PM
  5. incompatible types huh??
    By skanxalot in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 09:05 PM