Thread: warning: Incompatible integer to pointer conversion assigning to 'char *' to int

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    49

    warning: Incompatible integer to pointer conversion assigning to 'char *' to int

    Hi all, when I use the function to concat the pointer, the warning message is come out (Incompatible integer to pointer conversion assigning to 'char *' to 'int'), how can I fix this, many thanks!!

    Code:
    char* concat(char* left,char* right)
    {
        char* all = malloc(strlen(left)+strlen(right)+1);
        strcpy(all, left);
        strcat(all, right);
        puts(all);
        free(all);
        return all;
    }
    
    //use the concat function like this:
    char* all;
    char* tmp="abc";
    all = concat("06",tmp); //warning msg here "sc means concat"
    Last edited by homoon; 06-18-2012 at 03:54 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I notice that you are calling a mysterious function named sc, not your concat function.

    Besides this, there is the issue that you called strcpy before initialising what all points to, i.e.,
    Code:
    all[0] = '\0';
    should have come before the strcpy call. You should also check that malloc did not return a null pointer.

    Then, it makes no sense to free(all) then return all; Perhaps you should free in the caller instead.

    Oh, and since you are not modifying what left and right point to, you should declare them to be const char* instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > should have come before the strcpy call. You should also check that malloc did not return a null pointer.
    strcpy does not depend on the initial contents of the destination (whereas strcat does).
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Since you 'free' the memory inside the function don't expect the caller to have valid contents in the string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warning: incompatible pointer type
    By Cnewbi in forum C Programming
    Replies: 7
    Last Post: 12-29-2011, 03:17 PM
  2. How would I fix this incompatible pointer type warning
    By ogglock in forum C Programming
    Replies: 2
    Last Post: 07-11-2011, 01:17 AM
  3. [Warning] Incompatible pointer type
    By dgs012 in forum C Programming
    Replies: 5
    Last Post: 02-20-2011, 11:27 AM
  4. Incompatible Pointer Type Warning
    By kwikness in forum C Programming
    Replies: 5
    Last Post: 10-30-2007, 06:14 PM