1

I'm trying to pass an array of pointers to a function, without the need to define the size. Also the output is a warning "initialization makes pointer from integer without cast. Mostly interested in passing array of pointers rather than an array because I'm trying to understand how to deal with the instance when Data is a dynamically allocated memory using malloc

Why is this happening, and how can I pass an array of pointers as an argument, without the need to define its size?

Code:

#include <stdio.h>

void printData(int *data[]);


void main (int argc,char *argv[]){

    int *Data[] = {4, 8, 15, 16, 23, 42};
    printData(Data);

}

void printData(int *data[]){

    int **cur_data = data;
    int counter = sizeof(data)/sizeof(int);
    for(int i = 0; i<counter; i++){
        printf("%d. %d\n", i, *cur_data);
        cur_data++;
    }
}

Warning:

main.c: In function ‘main’:
main.c:9: warning: initialization makes pointer from integer without a cast
main.c:9: warning: initialization makes pointer from integer without a cast
main.c:9: warning: initialization makes pointer from integer without a cast
main.c:9: warning: initialization makes pointer from integer without a cast
main.c:9: warning: initialization makes pointer from integer without a cast
main.c:9: warning: initialization makes pointer from integer without a cast
gcc   main.o -o run

Run: 
0. 4
Iancovici
  • 5,574
  • 7
  • 39
  • 57

4 Answers4

3

You can't, an array decays into a pointer when is passed to a function, pass counter as a parameter to printData:

#include <stdio.h>

void printData(int *data, int counter);

int main(void)
{
    int Data[] = {4, 8, 15, 16, 23, 42};
    int counter = sizeof(Data) / sizeof(int);

    printData(Data, counter);
    return 0;
}

void printData(int *data, int counter)
{
    for (int i = 0; i < counter; i++){
        printf("%d. %d\n", i, data[i]);
    }
}

Or you can store the counter into the first element of array:

#include <stdio.h>

void printData(int *data);

int main(void)
{
    int Data[] = {0, 4, 8, 15, 16, 23, 42};

    Data[0] = sizeof(Data) / sizeof(int) - 1;
    printData(Data + 1);
    return 0;
}

void printData(int *data)
{
    int counter = data[-1];

    for (int i = 0; i < counter; i++){
        printf("%d. %d\n", i, data[i]);
    }
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94
1

Here's the corrected program, when you pass an array to a function, it becomes a pointer and loses its size information, so needs to pass separately

#include <stdio.h>

void printData(int *data, int size);


int main (int argc,char *argv[]){

    int Data[] = {4, 8, 15, 16, 23, 42};
    printData(Data, sizeof(Data));
    return 0;
}

void printData(int *data, int size){
    int *cur_data = data;
    int counter = size/sizeof(int);
    for(int i = 0; i<counter; i++){
        printf("%d. %d\n", i, *cur_data);
        cur_data++;
    }
}

and the output is:

0. 4
1. 8
2. 15
3. 16
4. 23
5. 42
Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
1

You declared array Data as an array of pointers but initialized it with integral constants instead of addresses.

int *Data[] = {4, 8, 15, 16, 23, 42};

So the compiler issues diagnostic messages.

You can do the task the following way that is by using a sentinal value that in this case is equal to NULL.

#include <stdio.h>
#include <stdlib.h>

void printData( int *data[] );


int main(void) 
{
    int Values[] = { 4, 8, 15, 16, 23, 42 };
    const size_t N = sizeof( Values ) / sizeof( *Values );
    int *Data[N+1];
    size_t i;

    i = 0;
    for ( ; i < N; i++ )
    {
        Data[i] = malloc( sizeof( int ) );
        *Data[i] = Values[i];
    }
    Data[i] = NULL;

    printData( Data );

    for ( i = 0; i < N + 1; i++ ) free( Data[i] );

    return 0;
}

void printData( int *data[] )
{
    int **p = data;
    size_t i = 0;

    for( ; *p != NULL; ++p, ++i )
    {
        printf("%d. %d\n", i, **p );
    }
}

The output is

0. 4
1. 8
2. 15
3. 16
4. 23
5. 42

Take into account that function main shall have return type int

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • If you use a variable length array, then you don't need to use `malloc`, anyway there is no need to (m)allocate for each value, a single `malloc` is enought: http://ideone.com/YN90yO – David Ranieri Aug 26 '14 at 12:30
  • @Alter Mann I used separate malloc under the expression that the original code initialize each element of the array.:) So initially I wanted to write something as int *Data = { malloc( sizeof( int ) ), malloc( sizeof( int ), /*...*? }; :) – Vlad from Moscow Aug 26 '14 at 12:38
0

Where-

int *Data[];

where data is a array of pointer to an integer. This array pointer can point to 1D arrays or integer. But you are filling with integer data. For 1D array use

int Data[]={4, 8, 15, 16, 23, 42};

And there is no way to know the size of the array using pointer! Only way is pass the no of element to the function with address of an array!

Try these changes-

#include <stdio.h>

void printData(int *data,int element);


void main (int argc,char *argv[]){

    int Data[] = {4, 8, 15, 16, 23, 42};
    printData(Data,6);

}

void printData(int *data, int element){

    int *cur_data = data;
    int counter = (sizeof(data)*element)/sizeof(int);
    for(int i = 0; i<counter; i++){
        printf("%d. %d\n", i, *cur_data);
        cur_data++;
    }
}
Sathish
  • 3,740
  • 1
  • 17
  • 28