Like Tree1Likes
  • 1 Post By Salem

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

This is a discussion on warning: Incompatible integer to pointer conversion assigning to 'char *' to int within the C Programming forums, part of the General Programming Boards category; Hi all, when I use the function to concat the pointer, the warning message is come out (Incompatible integer to ...

  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
    21,647
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    > 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).
    laserlight likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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, 02: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, 10:27 AM
  4. Incompatible Pointer Type Warning
    By kwikness in forum C Programming
    Replies: 5
    Last Post: 10-30-2007, 06:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21