Defining global struct array of pointers - c

im learning c programing.
I want to define a global struct array of. so I would have a pointer to that array that each member of the array is a struct of complex numbers.
my goal is to be able to acces to this array by his pointer (*vars) and being able to change/read its members on every function at the main.
Im facing troubles with this issue and im not sure how and where to define each thing.
I tried this next code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct complext
{
double real;
double img;
} complex;
complex* vars;
int main()
{
int i;
vars = malloc(6 * sizeof(vars));
for (i = 0; i < 6;)
vars[i]->real = 0;
}
Im getting an error when im trying to acces vars[i].
"request for member 'real' im something not a structure or a union.
Thanks!

There are 3 bugs in your code
1. vars[i]->real should be vars[i].real. Pls honor the data-types You have defined vars to be a global pointer to the complex structure. To define it as an array use: complex vars[6]; --> Look at #José Fonte 's ans
2. malloc returns a void * cast it to (complex*) --> I learnt this should be ok (look at comments posted by #Stargateur)
3. The for loop at the end has no inc statement for i hence it runs forever
4. The malloc only allocates 6 pointer worth of memory (which is 6*sizeof(int)) since vars is a pointer of type complex. sizeof(vars) should be sizeof(complex) --> #xing pointed this out
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef struct complext {
double real;
double img;
}complex;
complex *vars;
int main()
{
int i;
vars= (complex*)malloc(6*sizeof(complex));
for(i=0;i<6; i++)
vars[i].real=0;
}

Your mixing pointers with arrays. Doing it as an array (as a pointer already done by #Zakir):
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef struct complext {
double real;
double img;
}complex;
complex vars[6];
int main()
{
int i;
for(i=0;i<6;i++)
vars[i].real=0;
}

vars is of type struct complext *, but vars[i] is of type struct complext, so you just have to use vars[i].real instead of vars[i]->real.

Related

C Declare array of struct/pointer to array of structs

I have an example tutorial with 2 files, "functions.c" and "functions.h" which contain the prototypes and the body of the functions.
In the example there isn't the main that containing the declaration of the array of struct/pointer to array of structs and the calls to the functions.
functions.c:
#include "functions.h"
const char *getTeamA(const sTest *p)
{
return p->teamA;
}
void setTeamA(sTest *p, char *s)
{
strcpy(p->teamA, s);
}
int getNum(const sTest *p)
{
return p->num;
}
void setNum(sTest *p, int i)
{
p->num = i;
}
functions.h:
#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_CHAR 20
#define SIZE 5
typedef struct {
char teamA[MAX_CHAR];
int num;
// ...
} sTest;
const char *getTeamA(const sTest *p);
void setTeamA(sTest *p, char *s);
int getNum(const sTest *p);
void setNum(sTest *p, int i);
#endif /* FUNCTIONS_H_ */
So my question is
How can i declare the struct according to the code written above?
So for example:
int main()
{
sTest data[SIZE]; //size isn't important
sTest *dataPtr = data;
setTeamA(dataPtr[0].teamA, "name1");
// ...
printf("%d", getNum(dataPtr[1].num)); // just an example. i know that it isn't initialized
// ...
return 0;
}
Is this the correct way? Or is there a better way to declare variables and pass them to the functions?
The important thing is that i have to stick to the code written in functions.c and functions.h, so the functions cannot directly modify the struct data, you need to use pointers (because there are member selection operator "->" in functions.c).
You don't need to have dataPtr. You can do the exact same thing by doing data[i], since you declared data as an array of sTests, and so data points to the first element in the array.
Let's deconstruct what you're doing when you're calling setTeamA(dataPtr[0].teamA, "name1"). You're trying to set the first sTest struct in the data array to have "name1" as the teamA field. Notice that the prototype for setTeamA() actually takes in a sTest *p. In your example, you're passing in the teamA field. So what you really want to call is setTeamA(&dataPtr[0], "name1"). This translates to the pointer pointing to the data at dataPtr[0].
While this works, as I said before, the dataPtr is unecessary. So this is equivalent to:
setTeamA(&data[0], "name1").
Also worth noting, you can simply write:
setTeamA(data, "name1")
since data is already a pointer to the first element in the array.
Use
setTeamA(&data[0], "name1")
It indexes to index 0 and then takes the reference (which is a pointer) of the result therefore making the type an sTest* then the setTeamA function will do it's job setting the teamA field.
That dataPtr variable is useless here.

filling structures with a pointer C

We started programming in School and need to fill a structure via a pointer but we get this error:
'Schueler' has no member named 'Schueler'
We are using eclipse Indigo with the MinGW Compiler.
#include <stdio.h>
int main()
{
typedef struct Schueler{
char Vorname[10];
char Nachname[10];
}Schueler;
Schueler* vpName;
char cSchuelerVName[10]="Hans";
vpName->Schueler.Vorname=cSchuelerVName;
return 0;
}
Firstly you need to allocate memory to the structure pointer
vpName
The way the structure element Vorname is accessed is wrong.
It should be pointername->Structure_element_name
Vorname is a character array. = cannot be used to assign the value
modified code:
#include <stdio.h>
int main()
{
typedef struct Schueler{
char Vorname[10];
char Nachname[10];
}Schueler;
Schueler* vpName=(Schueler*)malloc(sizeof(Schueler));
char cSchuelerVName[10]="Hans";
// vpName->Schueler.Vorname=cSchuelerVName;
strcpy(vpName->Vorname,cSchuelerVName);
printf("vpName->Vorname=%s\n",vpName->Vorname);
return 0;
}

Cannot allocate memory for struct, error: request for member '****' in something not a structure or union

I am recently doing a school homework and I am stuck, in creating dynamic array of structs. I was looking everywhere, but no answer helped me, so can you please look at my code and help me, what is wrong?
I keep getting error:
C:\kof\test1\main.c|29|error: request for member 'lenght' in something not a structure or union|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct time
{
int mi;
int ho;
int dn;
int me;
int ro;
};
struct journey
{
int lenght;
struct time zac;
struct time kon;
int tank;
int price;
};
int main ()
{
struct journey *fail;
struct journey *b=(struct journey*) malloc (1*sizeof(struct journey));
fail=b;
fail.lenght=5;
return 0;
}
Since *fail is a pointer of type struct journey, you access its elements using the '->' notation and not using the . notation as stated here

How do I null an array of pointers that is pointing to a Structure in C (not C++)

I'm currently trying to Null an array of pointers that point to a structure. Any help or documentation would be nice. I am a beginner so please be as clear as possible.
Here is an example of my code. Sorry if I don't have this listed correctly, it's my first posting.enter code here
#include "stdlib.h"
enum boxtype
{
Card,
Mask,
};
typedef struct
{
enum boxtype type;
int L;
int H;
int x;
int y;
int Area;
Float ManBox;
Float WomanBox;
}Boxes;
typedef struct
{
Boxes Info;
float Hight;
}Male;
typedef struct
{
Boxes Info;
int Size;
}Female;
void main()
{
Man Male[100];
Woman Female[100];
Boxes *Spaces[600]; //This is the array of pointers that needs to be nulled.
}
You can initialize an array with an initializer list like so:
Boxes *Spaces[600] = { NULL };
All of the elements in the array will be set to NULL.
if you initialize with calloc() the memory will be zero'ed before it is returned.
for(int i=0;i<600;i++)
{
*Space[i]=NULL;
}
the previous given answer was also right. If u don't understand the previous code then u can try this one.

Is something like "typedef int arrChoice[5];" a global variable?

#include <stdio.h>
#include <conio.h>
typedef arrChoice[10] /*is this a global variable?*/
int main() {};
getch();
return 0;
}
its not done yet, but this is what i meant.
typedef is not a global variable, it's simply an alias for another type. I usually use them for function pointers when I'm passing those around because writing them out every time is annoying.
typedef int (*function)(int, int);
I also use them to define a structure, union, or enumeration as a type
typedef struct {
int x;
int y;
int z;
} Point;
typedef declares the new type not variable.
This might help you. In the code you posted here, there is a error. There are no statements in side main function. getch and return statements should be inside main function. I feel your code should be like this.
#include <stdio.h>
typedef int arrChoice; /* arrChoice is alias to int */
arrChoice a[10] ;/* array a is a global variable of integers*/
int main()
{
getch();
return 0;
}
please note that the purpose of typedef is to assign alternative names to existing types(int,float,double,etc.). The following statements are similar.
typedef arrChoice[10] is similar to typedef int[10];
When you try to refer arrChoice, then you get an error message
expected expression before 'arrChoice'.

Resources