using malloc for an array of character pointers

This is a discussion on using malloc for an array of character pointers within the C Programming forums, part of the General Programming Boards category; I am able to work with n instances of a structure in one mallocated area, but when I try to ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    11

    using malloc for an array of character pointers

    I am able to work with n instances of a structure in one mallocated area, but when I try to do the same thing with just character pointers, I get compiler errors about making integer from pointer without a cast. If I create a structure with just a character pointer in it, it works just fine... I am just not seeing something here!!!

    This works:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
        struct items
        {
            unsigned int item_1;
            unsigned int item_2;
        };
        struct items *items_ptr;
    unsignedint i = 0;
    unsignedint num = 0;
        items_ptr = (struct items *) malloc (3 * sizeof(struct items));
        if (items_ptr == NULL)
        {
                printf ("\nOut of memory - ending program.");
    return1;
        }
        for (i = 0; i < 3; i++)
        {
            num = num + 1;
            items_ptr[i].item_1 = num;
            items_ptr[i].item_2 = num + 10;
        }
        for (i = 0; i < 3; i++)
        {
            printf ("%u %u \n", items_ptr[i].item_1, items_ptr[i].item_2);
        }
        free (items_ptr);
        items_ptr = NULL;
        return 0;
    }
    This DOES NOT work!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
        char * items_ptr = NULL;
    unsignedint i = 0;
        char * one = "one";
        char * two = "two";
        char * three = "three";
        items_ptr = (char *) malloc (3 * sizeof(char *));
        if (items_ptr == NULL)
        {
                printf ("\nOut of memory - ending program.");
    return1;
        }
        items_ptr[0] = (char *) one;
        items_ptr[1] = two;
        items_ptr[2] = three;
        for (i = 0; i < 3; i++)
        {
            printf ("%s", items_ptr[i]);
        }
        free (items_ptr);
        items_ptr = NULL;
        return 0;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    you want a pointer to a char *

    Code:
    char ** items_ptr = NULL;
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    That gives me a warning about assignment from incompatible pointer type.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dtkokovoko View Post
    That gives me a warning about assignment from incompatible pointer type.
    it is no good idea to cast the return of malloc.
    Kurt

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Sorry! That was an experimentation relic. Once removed, your suggestion worked perfectly!!! I will have to dig deeper to understand what and why...

    Many thanks!!!

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Quote Originally Posted by dtkokovoko View Post
    That gives me a warning about assignment from incompatible pointer type.
    The code is now as shown below. Interestingly, any time I edit it, the first time I do a build, I get the warning above. The second time I do the build it completes without the warning.

    Is this normal?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
    	char ** items_ptr = NULL;
    unsignedint i = 0;
    	char * one = "one";
    	char * two = "two";
    	char * three = "three";
    	items_ptr = (char *) malloc (3 * sizeof(char *));
    	if (items_ptr == NULL)
    	{
    		printf ("\nOut of memory - ending program.");
    return1;
    	}
    	items_ptr[0] = one;
    	items_ptr[1] = two;
    	items_ptr[2] = three;
    	for (i = 0; i < 3; i++)
    	{
    		printf ("%s\n", items_ptr[i]);
    	}
    	free (items_ptr);
    	items_ptr = NULL;
        return 0;
    }

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Did you not read the reply?

    Code:
    char ** items_ptr
    Code:
    items_ptr = (char *) malloc
    Do not cast malloc or, if you do, make sure it's cast to the same type as the variable you're assigning to.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Quote Originally Posted by ledow View Post
    Did you not read the reply?

    Code:
    char ** items_ptr
    Code:
    items_ptr = (char *) malloc
    Do not cast malloc or, if you do, make sure it's cast to the same type as the variable you're assigning to.
    Yes, I read the reply.

    I thought he was referring to the "experimentation relic", the (char *) cast attempt, on the first version.

    Code:
        items_ptr[0] = (char *) one;
    I thought he meant I just needed to change the declaration to char **.

    I am still working on figuring out what char ** means, but, so far, I have not found it in my reference books, and my online search capability goes stupid when I include ** in a search. So far my understanding is that, as he said, what I need is a pointer to a char * (actually to an array of char), (is that what a char ** is? - I have no idea!) but I also thought that the cast before malloc related to how the allocated memory was parsed.

    Thank you for the next hint: both sides of the assignment need to be the same type.

  9. #9
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    934
    A char** is a pointer to a pointer to a char. While a char* is just a pointer to a char. You can have as many asterisks as you like, but i've yet to see a legitimate use for more than 2 levels of indirection.

    Edit: And by this i mean in the specific case of pointers to a basic type.
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Thank you for the explanation, and thank all of you for your guidance!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc and array of pointers
    By cfanatic in forum C Programming
    Replies: 11
    Last Post: 09-13-2012, 05:41 AM
  2. Array of character pointers
    By blindchicken11 in forum C Programming
    Replies: 5
    Last Post: 11-27-2011, 08:43 PM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. array of character pointers
    By neandrake in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2004, 01:56 AM
  5. array of character pointers
    By RedRum in forum C++ Programming
    Replies: 6
    Last Post: 05-06-2002, 10:05 PM

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