Issues with malloc, returning pointer, conflicting types

This is a discussion on Issues with malloc, returning pointer, conflicting types within the C Programming forums, part of the General Programming Boards category; I'm debugging an assignment due very soon, please help ASAP. The error is: Assignment makes pointer from integer without a ...

  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
    21,636
    You probably need to forward declare the function, e.g., in a header file.
    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
    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
    21,636
    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.
    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

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, 08: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, 07: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


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