0

I have a pointer to an array. I know how many number of items that array can hold but the length of each item is dynamic. So how to memset() the array in this case.

Int8 *data[4]; //array can hold maximum of 4 elements

Also I want to allocate memory for each item. How to do this?

3
  • 2
    You'd probably be best using calloc. That will allocate and memset the values, plus "feels right" for an array. Commented Jun 16, 2015 at 9:23
  • 1
    Isn't that rather an array of pointers? Commented Jun 16, 2015 at 9:43
  • "I have a pointer to an array." - no, you have an array of pointers. I concur with Undur. A pointer to an array would would like Int8 (*data)[4]; Commented Jun 16, 2015 at 9:45

3 Answers 3

1

First, you initialize the array at definition time by saying,

Int8 *data[4] = { NULL };  //NULLify all the elements.

Then, you need to use a loop to allocate memory to each element of the array, like

for (index = 0; index < 4; index++)
    data[index] = calloc(SIZE, sizeof(Int8)); //SIZE is a MACRO

FWIW, calloc() will returned "zero"ed memory (if success), so no need to memset() separately if you want the memory to be initialized to 0.

Obviously, you need to check for the success of the allocation.

Sign up to request clarification or add additional context in comments.

3 Comments

@WhozCraig Yes, absolutely. My connectivity was down, so late in modifying. Thanks. :-)
I suspected. This is as close to what I can fathom the question was about. so have an uptick =P
@WhozCraig Appreciate your time and effort for review and the suggestion. :-)
0

First I would sizeof the individual elements. Use a for loop and recurs the number of elements within your declaration.

Set the sizeof return equal to an integer so that you can access it, then memset with that integer for each element.

Int8 *somedata;

for (int i = 0; i < 4; i++) {    // for i less than 4 do increment i
    memset(data[i], somedata, sizeof(data[i]));   // memset data at point i, with value somedata, which has a size data[i] 
}

Hope that gives you something to go on!

1 Comment

sizeof( data[i] ) returns the size of the pointer, not what it's pointing to.
0
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main ( void )
{
        int *pointer_array[4],i=0;

/* Allocating memory and storing the address in pointer_array[] */
        for ( i=0; i<4; i++ )
        {
                pointer_array[i] = malloc ( sizeof(int) );
        }

/* Memset the values with 0 */
        for ( i=0 ;i<4; i++ )
        {
                memset( pointer_array[i], 0 , sizeof(int) );
        }
/*printing the values */
        for ( i=0 ;i<4; i++ )
        {
                printf ( "\n %d", *pointer_array[i] );
        }
/* deallocating the memory */
        for (i=0; i<4; i++ )
        {
                free ( pointer_array[i] );
        }

        return ( 0 );

}

output:
rabi@rabi-VirtualBox:~/rabi/c$ ./a.out 

 0
 0
 0
 0rabi@rabi-VirtualBox:~/rabi/c$

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.