error while traversing array into struct in c - c

I am trying to traversing an array into struct in C, but I got the error: error: expected identifier befoe '(' token
My Structs
typedef struct book {
int book_code;
char title[80];
char author[80];
InfoStudent status[2];
} Book;
typedef struct info_student {
char status_char;
char student_code[7];
int day_get;
int month_get;
int day_dev;
int month_dev;
} InfoStudent;
My function to save the data with error:
void create_book(Book *pbk) {
pbk->book_code= 1;
printf("Type the title:");
gets(pbk->title);
fflush(stdin);
printf("Type the author:");
gets(pbk->author);
fflush(stdin);
int i;
for (i=0; i<2; i++) {
pbk->(status+i)->day_get = -1; // Error line
pbk->(status+i)->day_dev = -1;
pbk->(status+i)->month_get = -1;
pbk->(status+i)->month_dev = -1;
pbk->(status+i)->student_code = '';
pbk->(status+i)->status_char ='F';
}
}
I am trying to set the status+0 values and status+1 values, using this for loop to optimize my writing code. I don't want use the "[" "]" to access the values.

Related

Incompatible pointer type when making structure of function pointers

I'm trying to make a array of structures which contain a string and a function pointer, however when I compile I get a warning that I've initialized from an incompatible pointer type.
I have no idea why (sorry if I sound ignorant, I'm fairly new to C programming).
typedef struct
{
char Player1[2], Player[2], **gameGrid;
int height,width;
int moveNum, player1Num, player2Num;
bool player1Win, player2Win;
}Game;
typedef int (*pointer_func)(Game *);
typedef struct
{
char *funcName;
pointer_func *f;
}userFunc;
int save(Game *struc);
int load(Game *struc);
int move(Game *struc);
int quit(Game *struc);
void free_grid(Game *struc);
int main(){
//initialised variables
userFunc Name_arr[] = {
{"save",save},
{"load",load},
{"quit",quit},
{"move",move}
};
The four functions being referenced are as follows:
int save(Game *struc)
{
char *str, *inputString, *writeString;
FILE *fp;
int nextPlayer;
int maxRead = 20;
bool DIRresponse;
while(true)
{
printf("Please provide a file name (20 characters max): ");
inputString = input_String(inputString, maxRead, stdin);
if((DIRresponse = check_Directory(inputString)) == true){
printf("That name already exists, choose another\n");
continue;
}
else
break;
}
if(struc->moveNum % 2 == 0)
nextPlayer = struc->player1Num;
else
nextPlayer = struc->player2Num;
sprintf(str,"%s.txt",inputString);
fp = fopen(str,"w");
sprintf(writeString, "%d %d %d %d %d", nextPlayer, struc->height,
struc->width, struc->moveNum, struc->moveNum);
fprintf(fp,writeString);
fclose(fp);
return 0;
}
int move(Game *struc)
{
return 1;
}
int load(Game *struc)
{
return 1;
}
int quit(Game *struc)
{
free_grid(struc);
exit(EXIT_SUCCESS);
}
You have a mismatch in levels of pointers:
typedef int (*pointer_func)(Game *); << Pointer type
typedef struct
{
char *funcName;
pointer_func *f; << Pointer to a pointer type.... OOPS
}userFunc;
Make *f -> f and it should work.

A function that return values from a structure in C

I have this struct in C language.
struct webit{
char indice[10];
int prid;
double latitude;
char date[20];
double longitude;
int xspeed;
int rEvt;
int alti;
int seq1;
int seq2;
int presi;
char cod1[5];
char cod2[5];
int iint1;
int iint2;
int x15;
};
and I have this function already putted before the struct webit
tramafunction();
this is how the function works: receive a string of delimited by commas and then split the data to a 15 diferent variables, this is an example of a string :
/*¶bL0 L3,01,+08590323,-079343001,010215,00000000000000,-tN,000,012689997,001219456,000,7FF2,C07F,0,4,*/
Function:
trama function(){
struct webit wbt;
char buf[103]="";
scanf("%[^\t\n]s",buf);
printf("\n \n Trama Recibida=[%s]\n\n", buf);
int z;
z=strlen(buf);
printf("TAMANO DE LA TRAMA: %d",z);
int i = 0;
char *p = strtok (buf, ",");
char *array[16]={0};
while (p != NULL)
{
array[i++] = p;
p = strtok (NULL, ",");
}
for (i = 0; i <16; ++i){
wbt.x15 = 0;
if (array[15] != NULL){
wbt.x15=atoi(array[15]);
}
printf("DATA: [%s]\n", array[i]);
}
strcpy(wbt.indice,array[0]);
printf("INDEX: [%s]\n",wbt.indice); /*AND EVENTUALLY WITH ALL OTHER DIFERENT DATA TYPES VARIABLES*/
}
in the main
int main()
{
tramafunction();
return 0;
}
At the end they convert the 15 data types of the array into the variables of webit in this case defined wbt. example "wbt.indice"
HOW CAN MAKE THE FUNCTION TRAMA RETURN ALL THE VALUES OF wbt. so I can use it anywhere in the programas, another function, another struct. etc

Implement a set using structure but it is giving errors [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
It is giving error in pushintoset function that array undeclared first use in this function
invalid type argument of '->' (have 'int')
#include <stdio.h>
#include <stdlib.h>
struct set
{
int data1;
int data2;
int size;
int *array;
int index;
};
struct set *createset(int capacity)
{
struct set * s = (struct set*)malloc(sizeof(struct set));
s->size = capacity;
s->array = (int *)malloc(sizeof(int) * s->size);
return s;
}
void pushintoset(struct set *st) //giving error in this function that array undeclared
{
int item1;
int item2;
int i;
for(i=0;i<5;i++)
{
printf("enter set %d \n",i);
scanf("%d %d",&item1,&item2);
st->index = i;
st->array[st->index]->data1 = item1;
st-array[st->index]->data2 = item2;
st->array[st->index]->index = i;
}
}
void printset(struct set * s)
{
int i;
for(i=1;i<=5;i++)
{
printf("%d \t %d \n",s->array[i]->data1,s->array[i]->data2);
}
}
int main()
{
struct set * se = createset(5);
if(se==NULL)``
printf("memory not allocate");
pushintoset(se);
printset(se);
return 0;
}
st-array[st->index]->data2 = item2; // loss of '>' after 'st-'
and you should not access struct member array as a struct point here.
Can you be more specific ?
What are you trying to do in these lines
st->array[st->index]->data1 = item1;
st-array[st->index]->data2 = item2;
st->array[st->index]->index = i;
st is a pointer to struct set and you can access its variable
st-> structure member
you can access data1 and data2 this way
st->data1 and st->data2
data1 and data2 is part of set structure. They are not member of st->array. st->array is type of int *. You should try st->data1 , st->data2.
You try to access
st->array[st->index]->data1
st->array[st->index] is of type int, you can't access ->data2?
In your main you instantiate a strut set with an internal array size of 5, then you pass it to pushIntToSet(), the code should be instead:
void pushintoset(struct set *st)
{
int item1;
int item2;
int i;
if (set == NULL) //checking for null ptr
return ;
printf("Enter two numbers for set [%d] \n", i);
scanf("%d %d", &item1, &item2);
st->data1 = item1;
st->data2 = item2;
}
It's probably not what you wanted to do, but the array is just an int array and hae nothing to do with data1/data2.
You are creating a single pointer array and instead of storing the values directly you are storing int type.
see the below code it is working fine.
#include<stdio.h>
#include<stdlib.h>
struct set
{
int data1;
int data2;
int size;
int *array;
int index;
};
struct set *createset(int capacity)
{
struct set * s = (struct set*)malloc(sizeof(struct set));
s->size = capacity;
s->array = (int *)malloc(sizeof(int) * s->size);
return s;
}
void pushintoset(struct set *st) //giving error in this function that array undeclared
{
int item1;
int item2;
int i;
for(i=0;i<5;i++)
{
printf("enter set %d \n",i);
scanf("%d %d",&item1,&item2);
st->index = i;
st->array[st->index] = item1;
st->array[st->index] = item2;
st->array[st->index] = i;
}
}
void printset(struct set * s)
{
int i;
for(i=1;i<=5;i++)
{
printf("%d \t %d \n",s->array[i],s->array[i]);
}
}
int main()
{
struct set * se = createset(5);
if(se==NULL)
printf("memory not allocate");
pushintoset(se);
printset(se);
return 0;
}
see it on live here
https://ideone.com/TXBpsP
When you look at the three lines:
st->array[st->index]->data1 = item1;
st-array[st->index]->data2 = item2;
st->array[st->index]->index = i;
You should spot the misalignment.
st - array
references the variable array, which is not defined. That's why you're getting the mention of array being undefined. You're missing the > necessary to make it compile.
When you fix that, you have a problem that st->array is an integer pointer; you're trying to treat it as an array of pointers to your structure type.
void pushintoset(struct set *st) //giving error in this function that array undeclared
{
int item1;
int item2;
int i;
for(i=0;i<5;i++)
{
printf("enter set %d \n",i);
scanf("%d %d",&item1,&item2);
st->index = i;
// st->array[st->index] is ok
// but st->array[st->index]->data1 is wrong
// array is a pointer of int as your define,
// it isn't a strcut, have no member data1 and data2
// and the other probelm, wild pointer!
// you use it as a array without initialize it (Memory allocation)
st->array[st->index]->data1 = item1; // ??
st-array[st->index]->data2 = item2;
st->array[st->index]->index = i;
}
}

Why am I getting "ERROR : request for member stringLength and name in something not a structure or union"?

I'm trying to implement a stack and its basic properties (push, pop, etc.) but I'm getting the error I mentioned in the title:
ERROR : request for member stringLength and name in something not a structure or union
#include <stdio.h>
#include <stdlib.h>
typedef struct stackElement
{
int stringLength;
char *name;
} StackElement;
int Push(StackElement **stack);
int main()
{
StackElement *stack = NULL;
int index = 0;
index = Push(&stack);
printf("The top word of the stack is %s\n", stack[index].name);
system("PAUSE");
return 0;
}
int Push(StackElement **stack)
{
char *c;
int size = 0;
int i = 0;
*stack = malloc(sizeof(StackElement));
printf("Please enter a word in the stack\n");
scanf("%s",&c);
size = sizeof(c)/sizeof(char);
*stack[i].stringLength = size;// <---- ERROR
*stack[i].name = c ;// <----- ERROR
return i;
}
It's an operator precedence issue.
. has higher precedence than *, so:
*stack[i].stringLength
Is the same as
*(stack[i].stringLength)
While you actually want
(*stack[i]).stringLength
Just add the brackets as I did in the last example and it should work.

"struct" objects with "for-loop" in C Programming

This code is about 'struct' in C..
I created a struct spieler with the properties name and age..
By using the for-loop I let the user to create the struct objects.
they are named as sp[i] --> sp1, sp2 etc.
the problem is the objects are created. But I can use them only inside the for-loop.
If I want to get the value of "sp1.name" in main function, it doesn't work.
How can I solve it?
struct spieler{
char name[20];
int age;
};
void erzeuge();
int main() {
int anzahl = 2;
printf("Anzahl Spielern: ");
scanf("%d",&anzahl);
erzeuge(anzahl);
printf("Es sind %d Spielern",anzahl);
/*for(i;i<anzahl;i++){
printf("%d.%s",i, sp[i].name);
}*/
getchar();
}
void erzeuge(int anzahl){
int i=0;
for(i;i<anzahl;i++){
struct spieler sp[i];
printf("Struct fuer Spieler_%d wurde erzeugt\n", i);
getchar();
printf("Name: ");
scanf("%s",sp[i].name);
printf("%s\n",sp[i].name);
}
You should declare sp as a pointer at the global scope, and allocate memory for it inside the function erzeuge using malloc.
#include <stdlib.h>
#include <stdio.h>
struct spieler {
char name[20];
int age;
};
struct spieler *sp; // Add this
void erzeuge();
int main() {
int anzahl;
printf("Anzahl Spielern: ");
scanf("%d", &anzahl);
erzeuge(anzahl);
printf("Es sind %d Spielern\n", anzahl);
int i;
for(i = 0; i < anzahl; i++){
printf("%d.%s\n", i, sp[i].name);
}
if (sp) {
free(sp);
}
getchar();
return 0;
}
void erzeuge(int anzahl) {
// Add the following line to allocate memory
sp = (struct spieler*) malloc(anzahl * sizeof(struct spieler));
int i;
for (i = 0; i < anzahl; i++) {
// Remove the following line because it create an array of "i" elements
// struct spieler sp[i];
printf("Struct fuer Spieler_%d wurde erzeugt\n", i);
getchar();
printf("Name: ");
scanf("%s",sp[i].name);
printf("%s\n",sp[i].name);
}
}
You'd have to return an array of players from erzeuge, something like
struct spieler *erzeuge(int anzahl){
struct spieler *mannschaft = malloc(anzahl*sizeof(struct spieler));
int i;
for(i = 0; i < anzahl; ++i){
// prompt
scanf("%18s",&mannschaft[i].name);
...
}
return mannschaft;
}
Alternative solution without malloc:
void erzeuge(struct spieler* sp, int anzahl)
{
...
}
int main()
{
int anzahl = 2;
...
struct spieler sp[anzahl];
erzeuge(sp,anzahl);
...
}

Resources