Thread: arrays dimensioning: malloc and sizeof

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    3

    arrays dimensioning: malloc and sizeof

    I'm not sure whether I'm doing this wrong or sizeof can't read arrays dimensioned with the memory allocation functions. Here's what I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void main()
    {	char *NEW_ARRAY;
    	char NEW_ARRAY2[5];
    
    	NEW_ARRAY = malloc(5 * sizeof(char));
    
    	if(NEW_ARRAY != NULL)
    	{	printf("&#37;d\n", sizeof(NEW_ARRAY));
    		printf("%d\n", sizeof(NEW_ARRAY2));
    	}
    
    	free(NEW_ARRAY);
    }
    NEW_ARRAY returns 4 (no matter what I put in as the MALLOC size value)
    NEW_ARRAY2 returns the correct number of 5 positions within the array.

    Any ideas? The solution I'm using this in pulls characters from a static array. The amount of characters it pulls varies so I need to create a dynamic container character array.
    Last edited by c programmer; 03-27-2007 at 03:38 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc and sizeof question
    By cus in forum C Programming
    Replies: 4
    Last Post: 01-03-2009, 12:23 PM
  2. More malloc questions...
    By kermit in forum C Programming
    Replies: 3
    Last Post: 09-13-2008, 06:25 PM
  3. malloc sizeof struct
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 07:49 AM
  4. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  5. Need help on pointer to VOID
    By dv007 in forum C Programming
    Replies: 19
    Last Post: 07-09-2002, 06:15 PM