Thread: strcat problem

  1. #1
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153

    strcat problem

    Seems so simple but I have been pulling my hair out:
    I'm just trying to create one long char from 2 short chars. Looks like this:
    Code:
     // IN STRUCT DEFINITION 
    // char imagename[30];
    
    char dir[40];
    dir = strcat( "pngimg/", option.imagename );
    Compiler: incompatible types when assigning to type âchar[40]â from type âchar *â

    I feel its some kind of pointer dereferencing thing, but I'm still a noob with pointers. Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You are trying to use a constant string for your destination string ("pngimg/"), you probably have your parameters reversed.

    Jim

  3. #3
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Tried reversing parameters, same problem. Any other ideas? Thank you.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You can't assign to an array like that.

    Code:
    #include <stdio.h>
    ...
    char dir[40]; // Probably too small
    sprintf("pngimg/%s", option.imagename);

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How is option.imagename defined? You probably need to copy the first part of your "new" string to your dir variable with strcpy(), then use strcat() to append option.imagename to dir. Something like:
    Code:
    char dir[40];
    strcpy(dir, "pngimg/");
    strcat( dir, option.imagename );
    Make sure "dir" is big enough to hold your two strings.

    Jim

  6. #6
    Registered User javaeyes's Avatar
    Join Date
    Feb 2012
    Posts
    153
    Thank you jim blumberg!!! You're the man.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Damn, I hosed that code...

    Code:
    #include <stdio.h>
    ...
    char dir[40]; // Probably too small
    sprintf(dir, "pngimg/%s", option.imagename);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with strcat
    By morngrym in forum C++ Programming
    Replies: 12
    Last Post: 02-20-2010, 03:57 AM
  2. strcat problem
    By mindtrap in forum C Programming
    Replies: 5
    Last Post: 08-10-2007, 02:39 AM
  3. Problem with strcat
    By firyace in forum C Programming
    Replies: 9
    Last Post: 05-15-2007, 02:31 PM
  4. problem with strcat
    By Nanook in forum C Programming
    Replies: 7
    Last Post: 09-04-2005, 03:51 AM
  5. strcat problem..
    By cnewbee in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 09:06 AM