Thread: ERROR: assignment makes integer from pointer without a cast

  1. #1
    C Fanatic ehitam's Avatar
    Join Date
    Jun 2012
    Posts
    22

    ERROR: assignment makes integer from pointer without a cast

    Code:
    char *mid(char *tString, int x1, int x2){
        char *rString;
        int i, l;
        rString= (char *) malloc(sizeof(char) * x2);
    
    
        l = x1-1;
        for (i= 0; i<x2; i++)
        {
            rString[i] = tString[l];
            l++;
            }
            rString[x2]= NULL;
    
    
        return rString;
        }
    Mingw is giving me this error:
    C:\Users\Harshvardhan\Desktop\RPL\functions.h|30|w arning: assignment makes integer from pointer without a cast [enabled by default]|

    and for this line:
    Code:
    rString[x2]= NULL;
    And possible reason/cure for this?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    rString is a pointer (presumably to an array of chars or a string). Therefore rString[x2] is a single char. But NULL is a pointer type, which is not compatible with char. You are probably looking for the null byte/character (yes, slightly confusing) to terminate your string. Try
    Code:
    rString[x2] = '\0';

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    NULL is a null pointer constant. While it could just be the integer 0, it could also be (void*)0 or something else that ultimately is a null pointer constant.

    What you really want is a character constant, so just write:
    Code:
    rString[x2] = '\0';
    By the way, as a single letter name, l is a horrible name as it can be confused with 1 on some fonts.
    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

  4. #4
    C Fanatic ehitam's Avatar
    Join Date
    Jun 2012
    Posts
    22
    lol... And I always thought that NULL is exactly same as '\0'


  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ehitam
    I always thought that NULL is exactly same as '\0'
    On some implementations, it is. Except for spelling, 0 is identical to '\0'. But you should not rely on that.
    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

  6. #6
    C Fanatic ehitam's Avatar
    Join Date
    Jun 2012
    Posts
    22
    Quote Originally Posted by laserlight View Post
    By the way, as a single letter name, l is a horrible name as it can be confused with 1 on some fonts.
    I agree... I used it to signify length... And in code::blocks it is same as 1.

    I think I should change it with something better.

    And...

    Thanks laserlight and anduril for help...

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by laserlight View Post
    NULL is a null pointer constant. While it could just be the integer 0, it could also be (void*)0 or something else that ultimately is a null pointer constant.

    What you really want is a character constant, so just write:
    Code:
    rString[x2] = '\0';
    By the way, as a single letter name, l is a horrible name as it can be confused with 1 on some fonts.
    Is this a past end of array?

    In other words, is the code below correct?

    Code:
    rString[x2-1] = '\0';
    Really tired, so my thinking might be wrong.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It looks more likely that the malloc should have allocated another char.
    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

  9. #9
    C Fanatic ehitam's Avatar
    Join Date
    Jun 2012
    Posts
    22
    Quote Originally Posted by stahta01 View Post
    Is this a past end of array?
    No... x2-1 just make a character less... x2 is fine.

  10. #10
    C Fanatic ehitam's Avatar
    Join Date
    Jun 2012
    Posts
    22
    Quote Originally Posted by laserlight View Post
    It looks more likely that the malloc should have allocated another char.
    Do you mean rString?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ehitam
    x2 is fine.
    The problem is that stahta01 is correct to say that it is a problem: you're writing to the array out of bounds. If you want to do this, your malloc should have a +1.
    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

  12. #12
    C Fanatic ehitam's Avatar
    Join Date
    Jun 2012
    Posts
    22
    Quote Originally Posted by laserlight View Post
    The problem is that stahta01 is correct to say that it is a problem: you're writing to the array out of bounds. If you want to do this, your malloc should have a +1.
    Sorry... ... I'm not getting what you mean to say...

    When I use x2-1.... I get a character less...

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, you get a character less, but it's better than undefined behavior (what array out of bounds accesses result in). Arrays in C start at index 0 and go to index SIZE-1, or in your case x2-1. That is, your current code allocates an array of x2 characters, meaning you have rString[0]...rString[x2-1]. That's it, no more. rString[x2] is out of bounds. Tim's code corrects that in one possible way (by stopping at x2-1). Laserlight suggested another way, which allocates x2+1 characters, so rString[x2] is valid.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I mean that instead of this:
    Code:
    rString= (char *) malloc(sizeof(char) * x2);
    you should write:
    Code:
    rString = malloc(sizeof(*rString) * x2 + 1);
    Notice the +1 to account for the null character.
    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

  15. #15
    C Fanatic ehitam's Avatar
    Join Date
    Jun 2012
    Posts
    22
    oh! Really didn't noticed that
    Thank you for help... I would replace x2 with x2+1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment makes pointer from integer without a cast
    By deciel in forum C Programming
    Replies: 4
    Last Post: 12-13-2011, 04:29 PM
  2. assignment makes pointer from integer without a cast
    By mant1s in forum C Programming
    Replies: 4
    Last Post: 10-07-2010, 01:25 AM
  3. Replies: 1
    Last Post: 08-11-2009, 12:09 PM
  4. assignment makes pointer from integer without a cast
    By lithium in forum C Programming
    Replies: 1
    Last Post: 04-02-2003, 11:37 PM
  5. Replies: 3
    Last Post: 01-14-2002, 12:13 PM