Thread: Issues with malloc, returning pointer, conflicting types

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    12

    Issues with malloc, returning pointer, conflicting types

    I'm debugging an assignment due very soon, please help ASAP.


    The error is:

    Assignment makes pointer from integer without a cast.

    So, apparently add_group returns an int, yet the return type is char *


    this is the block of code that calls the function which returns a char * pointer:

    buffer is delcared as this -------> char *buffer;

    Code:
    } else if (strcmp(cmd_argv[0], "add_group") == 0 && cmd_argc == 2) {
            buffer = add_group(group_list_addr, cmd_argv[1]);  
            printf("%s", buf);

    This is the block of code that is add_group:
    Code:
    char *add_group(Group **group_list_ptr, const char *group_name) {
        
        
       if (find_group(*group_list_ptr, group_name) == NULL) {
            //malloc space for new group
            Group *newgrp;
            char *groupSucc; 
            char *groupFail;
            if ((groupSucc = malloc(sizeof(group_name) + 21)) == NULL) {
                perror("Error allocating space for add_group success message");
                exit(1);
            strcpy(groupSucc, "Group ");
            strcat(groupSucc, group_name);
            strcat(groupSucc, " added");
            if ((newgrp = malloc(sizeof(Group))) == NULL) {
               perror("Error allocating space for new Group");
               exit(1);
            }
            // set the fields of the new group node
            // first allocate space for the name
            int needed_space = strlen(group_name) + 1;
            if (( newgrp->name = malloc(needed_space)) == NULL) {
               perror("Error allocating space for new Group name");
               exit(1);
            }
            strncpy(newgrp->name, group_name, needed_space);
            newgrp->users = NULL;
            newgrp->xcts = NULL;
            newgrp->next = NULL;
            // Need to insert into the end of the list not the front
            if (*group_list_ptr == NULL) {
              // set new head to this new group -- the list was previously empty
              *group_list_ptr = newgrp;
              return groupSucc;  
            }  else {
            // walk along the list until we find the currently last group 
              Group * current = *group_list_ptr;
              while (current->next != NULL) {
                current = current->next;
              }
              // now current should be the last group 
              current->next = newgrp;
              return groupSucc;
            }
        } else {
                // group EXISTS
                if ((groupFail = malloc(sizeof(group_name) + 21)) == NULL) {
                    perror("Error allocating space for add_group failure message");
                    exit(1);
                }
                strcpy(groupFail, "Group ");
                strcat(groupFail, group_name);
                strcat(groupFail, " already exists");
                return groupFail;
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You probably need to forward declare the function, e.g., in a header file.
    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
    Apr 2013
    Posts
    12
    Taking out the * on char *buffer; fixed it

    Still not sure whether I am returning what I actually want. The print statement thinks that buffer is an int?!!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by pointingmachine
    Taking out the * on char *buffer; fixed it
    Not likely. The compiler is not complaining, but your code is most certainly incorrect.

    As I said, the problem is likely because you did not declare the function before use. For example, you might be calling the function in the main function, whereas the function is defined after the main function.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conflicting types
    By Sotiris Kaniras in forum C Programming
    Replies: 27
    Last Post: 01-01-2013, 09:18 AM
  2. conflicting types for aprintfa
    By leo2008 in forum C Programming
    Replies: 2
    Last Post: 03-12-2012, 09:33 AM
  3. Error! Conflicting Types
    By davewang in forum C Programming
    Replies: 5
    Last Post: 12-05-2008, 08:47 AM
  4. Conflicting types???
    By kwikness in forum C Programming
    Replies: 11
    Last Post: 10-07-2007, 11:53 PM
  5. conflicting types for...
    By dudinka in forum C Programming
    Replies: 3
    Last Post: 05-14-2005, 07:03 AM

Tags for this Thread