Msalvatori
Msalvatori

Reputation: 37

Having trouble with array struct in C

I'm getting an error that says: "two or more data types in declaration specifiers" i've read that it happens when people forget to put ";" in the end of a struct, but as you can see, it has the ";". So, does anyone have an idea of what can generate this error?

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

typedef struct{
     char valorArray;
} arrayStruct; 

int main(void){

  arrayStruct char array[10];
  int i;
  int *pA;
  int *pP;
  for (i = 0; i < 10; i++){
      printf("Digite uma letra qualquer:\n");
      scanf("%c", &(array[i].valorArray));
      scanf("\n");

      printf("a letra na casa %d do array eh: %c\n", i, array[i].valorArray); 

      pA = &array[i].valorArray;
      printf("o endereco da posicao atual eh: %d\n", pA);

      pP = &array[i+1].valorArray;
      printf("o endereco da proxima posicao eh: %d\n\n\n\n", pP);
  } 

  system("PAUSE");  
  return 0;
}

Upvotes: 0

Views: 59

Answers (2)

Vishal R
Vishal R

Reputation: 1074

Below changes need to be done.

arrayStruct char array[10];

Change to because arrayStruct is a user-defined datatype:

arrayStruct array[10]; // example int arr[10] - int is a data type. 

Other logic change which I feel should be done is :

int *pA; // change to char *pA
int *pP; // change to char *pB
      pA = &array[i].valorArray;
      printf("o endereco da posicao atual eh: %d\n", pA);

      pP = &array[i+1].valorArray;
      printf("o endereco da proxima posicao eh: %d\n\n\n\n", pP);

You defined valorArray with char datatype so pA and pP should be char * instead of int *.

Hope this helps.

Upvotes: 0

TheWalkingCube
TheWalkingCube

Reputation: 2116

It's this line :

    arrayStruct char array[10];

Should be :

    arrayStruct array[10];

Upvotes: 2

Related Questions