I'm new to C and I'm trying to become familiar with the basics, I thought id make a program that just takes an array and uses a function to print the information in the array. However, when I try to compile this program I get the following error:
error: subscripted value is neither array nor pointer nor vector
printf(" %d", data[i]);
^
The code I'm currently using is below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int maxReturn(int a, int b, int c, int d);
int main(){
int arr[4] = {1,4,8,2};
int max = maxReturn(arr[0],arr[1],arr[2],arr[3]);
printf("%d\n", max);
return 0;
}
int maxReturn(int a, int b, int c, int d){
int data = {a,b,c,d};
for(int i; i < 4; i++){
printf(" %d", data[i]);
}
printf("\n");
return data;
}
I'm at a bit of a loss as to what is wrong here because in the tutorials I'm following they print out values by using data[i]. Why is this not ok?
You are declaring an int
using the {...}
construct as if you were initializing an array. Probably, you forgot to declare data
as an array of int
.
Replacing the line:
int data = {a,b,c,d};
with:
int data[4] = {a,b,c,d};
should fix your problem.
EDIT:
You might not even define the size of the array in this case, because using the {...}
construct the compiler computes the size of the array at build-time.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.