1

I have to write a function that will add an adress at the end of an array of pointers. Here is what I've done. I want to know if I did right and if not, please correct me.

#include <stdio.h>
#include <stdlib.h>
#define MAX 3

void add( int *array[MAX], int *addr)
{
    array = realloc(array, 1*sizeof(int));
    array[MAX+1] = addr;
}

int main()
{
    int *addr = 4;
    int *array[MAX] = {"1","2","3"};
    add(array, addr);
    int i;
    for(i = 0; i<4;i++)
        printf("%d ", array[i]);

    return 0;
}

1 Answers1

1

from the manual for realloc:

The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.

if its too long to read ill explain.

first of all you should use realloc after allocating memory ( using malloc for example ) and not after local declaration

second you're treating pointers to int ( int * ) as if they we're int. also shown as warning

examples:

int *addr = 4; 
int *array[MAX] = {"1","2","3"}; 
array = realloc(array, 1*sizeof(int));     // here you're using sizeof( int )

another problem is reaching out of bound of array

 array[MAX+1] = addr;

for an array with 3 spaces - you have array[ 0 ], array[ 1 ] and array[ 2 ].

in this line you're trying to reach the array[ 4 ] of an array ( that's suppose to be ) of size 4 --> out of bounds

my suggested code for this will be:

code edited

#include <stdio.h>
#include <stdlib.h>
#define MAX 3

void add( int **array[ MAX ], int *addr )
{
    *array = realloc( *array, ( MAX + 1 ) * sizeof( int* ) );
    (*array)[ MAX ] = addr;
}

int main()
{
    int i;
    int *addr;
    int **array;

    addr = &i;
    array = malloc( MAX * sizeof ( int* ) );
    for ( i = 1; i <= MAX; i++ ) {
        array[ i - 1 ] = addr + 4 * i;
    }

    add( &array, addr );

    for ( i = 0; i < MAX + 1; i++ ) {
        printf( "%p ", array[ i ] );
    }

    return 0;
}
Itay Sela
  • 942
  • 9
  • 26
  • I understood,thank you. But, my task says that my function add should add an address to this array of pointers. Your function seems to add the value. And will the same work with strings? For example, If I want to add a string address to an array of pointers? – Vlad Dohotaru Aug 30 '15 at 08:57
  • 2
    This is still not completely correct. The `add` function will not change the array that was passed in to it, it only changes its local variable `array`. – interjay Aug 30 '15 at 09:33
  • 1
    `*array[ MAX ] = addr;` should be `(*array)[ MAX ] = addr;` – BLUEPIXY Aug 30 '15 at 17:30