Thread: Malloc Pointer Issue

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Malloc Pointer Issue

    Hey guys, small issue that I'm sure has a quick fix, but I'm stumped. I'm getting this error:
    vector.c:20:13: warning: assignment from incompatible pointer type [enabled by default]
    vector.c:24:12: warning: assignment from incompatible pointer type [enabled by default]

    Code:
    void initialize(int **crns, char ***subjects, char ***courses, int *size)
    {
            *crns = (int *)malloc(100*sizeof(int));
            **subjects = (char **)malloc(100*sizeof(char*));
            int i;
            for (i = 0; i < 100; i++)
                    *(subjects)[i] = (char*)malloc(4*sizeof(char));
            **courses = (char **)malloc(100*sizeof(char*));
            for (i = 0; i < 100; i++)
                    *(courses)[i] = (char*)malloc(6*sizeof(char));
    
    
            *size = 100;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    There is no need to explicitly cast the return value of malloc here since there is an implicit conversion from void* to the various pointer types involved.

    As for your problem: I suggest that you first attempt to eliminate your use of char***. That is typically too many levels of indirection and makes things confusing. If necessary, create a structs to hold the data.
    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
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    First, I second laserlight's advice.

    Quote Originally Posted by stefanturk View Post
    Hey guys, small issue that I'm sure has a quick fix, but I'm stumped. I'm getting this error:
    vector.c:20:13: warning: assignment from incompatible pointer type [enabled by default]
    vector.c:24:12: warning: assignment from incompatible pointer type [enabled by default]
    I really appreciate that you've copied the original errors/warnings but it would be even better if you mark the corresponding lines in your code. That will make it easier for us to locate the lines because since you have only posted an extract of your program the line numbers from the compiler messages and the line numbers in the posted code don't match.

    Assuming that you call your function like
    Code:
    initialize(&crns, &subjects, &courses, &size);
    i.e. "subjects" and "courses" are two-dimensional arrays, "crns" is an one-dimensional array, and "size" is an int, I see 4 problematic lines in your code, I guess the compiler warnings are about these two lines:
    Code:
    **subjects = (char **)malloc(100*sizeof(char*));
    **courses = (char **)malloc(100*sizeof(char*));
    You have one indirection too much on the left side. "**subjects" is of type "char *" but your malloc returns a "char **".
    You need to write these lines as
    Code:
    *subjects = malloc(100*sizeof(char*));
    *courses = malloc(100*sizeof(char*));
    or even better
    Code:
    *subjects = malloc(100*sizeof(**subjects));
    *courses = malloc(100*sizeof(**courses));
    because that's the idiomatic way to allocate memory for an dynamic array:
    Code:
    ptr = malloc(size * sizeof(*ptr));
    In your case "ptr" is "*subjects" thus "*ptr" is "*(*subjects)" which is the same as "**subjects".

    I also think that the following two lines are wrong:
    Code:
    *(subjects)[i] = (char*)malloc(4*sizeof(char));
    *(courses)[i] = (char*)malloc(6*sizeof(char));
    Your parentheses aren't correct on the left side. As you have written them the compiler interprets them as
    Code:
    *(subjects[i])
    *(courses[i])
    because array subscripting [] has higher precedence than indirection *.
    You have to put the asterix inside the parentheses because you first have to dereference "subjects" and "courses" to get a pointer to pointer (i.e. a two-dimensional array):
    Code:
    (*subjects)[i]
    (*courses)[i]
    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to Pointer manipulation issue
    By workisnotfun in forum C Programming
    Replies: 5
    Last Post: 10-16-2012, 11:31 AM
  2. Serious issue with malloc function
    By anandvn in forum C Programming
    Replies: 14
    Last Post: 08-14-2010, 12:55 PM
  3. pointer issue
    By san_crazy in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:05 PM
  4. Replies: 6
    Last Post: 06-16-2008, 04:06 AM
  5. A bug I cannot see. . . malloc issue
    By Kennedy in forum C Programming
    Replies: 16
    Last Post: 09-26-2007, 09:49 AM