1

here's a simple and short code I've been trying to run:

#include <stdio.h>

int const SIZE = 5;

void a(int *arr);

int main(){
    int arr[5] = {1,2,3,4,5};
    a(arr);
    return 0;
}

void a(int *arr){
    int *i;
    for (i=arr; i<&a[5]; i++)
            printf("%d",*arr[i]);
}

and i get the following errors/warnings:

main.c: In function ‘main’:
main.c:15: error: variable-sized object may not be initialized
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c:15: warning: excess elements in array initializer
main.c:15: warning: (near initialization for ‘arr’)
main.c: In function ‘a’:
main.c:22: error: subscripted value is neither array nor pointer
main.c:23: error: array subscript is not an integer

the warning are all associated with the initialization of the array: if I put '5' instead of 'SIZE' its ok, why? The errors in a I don't get at all. I'm passing a pointer as an arguments, where's the problem? thatnks!

yotamoo
  • 5,334
  • 13
  • 49
  • 61

5 Answers5

5

Firstly, that should be:

for (i=arr; i<&arr[5]; i++)
               ^^^

But secondly, i is not an index, it's a pointer. So your print statement should be:

printf("%d",*i);
Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
1

Your code should presumably be:

void a(int *arr)
{
    int *i;
    for (i=arr; i < &arr[5]; i++)
            printf("%d",*i);
}

but why don't you just write the more understandable:

void PrintArray(int *arr, int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d", arr[i]);
}
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
0

Try this:

void a(int *arr, int size);

int main(){
    int arr[5] = {1,2,3,4,5};
    a(arr, 5);
    return 0;
}

void a(int *arr, int size){
    int i;
    for (i=0; i< size; i++)
        printf("%d", arr[i]);
}
beatgammit
  • 19,817
  • 19
  • 86
  • 129
0

Arrays in C are indexed from zero. So the last element is arr[4], not arr[5]

Géal
  • 1,482
  • 11
  • 13
0

In C89 array-dimensions must known to compile-time, the content of an variable is'nt known at compile-time (also it is const), a define can known at compile-time. a strictly example show like this:

#include <stdio.h>

#define MY_ARRAY_SIZE 5  /* "SIZE" is a bad name for a #define */

typedef int MY_TYPE[MY_ARRAY_SIZE]; /* if possible, use your OWN types for your usecases */

void my_function(int *arr); /* "a" is a bad name for a function */

int main(){
    MY_TYPE arr = {1,2,3,4,5};
    my_function(arr);
    return 0;
}

void my_function(MY_TYPE arr){
    int i;
    for (i=0; i<sizeof(MY_TYPE)/sizeof*arr; i++)
            printf("%d",arr[i]);
}
user411313
  • 3,930
  • 19
  • 16