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;
}
Related
This question already has answers here:
use malloc out of main
(3 answers)
Closed 4 years ago.
I'm trying to run this program that manage login but I have a problem with declaring this pointer as a global variable , the error that it shows me is
"initializer element is not constant"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char nom_utilisateur[20];
char mot_de_passe[20];
char nom[20];
char prenom[20];
}USER;
USER *u =(USER *)malloc(sizeof(USER)*20);
int nbr_usr=0;
void adduser() {};
......
In C you cannot call functions during initialisation of global variables.
You need to do the initialisation for example in main:
...
USER *u;
...
int main()
{
u = malloc(sizeof(USER)*20);
...
}
BTW:
you don't need to cast the result of malloc.
the variable name u is not a good idea. Give it a meaningful name, e.g. users.
Not directly related to your question, but be aware that the 20 in the struct declaration below is not at all related to the 20 in malloc(sizeof(USER)*20).
typedef struct {
char nom_utilisateur[20];
char mot_de_passe[20];
char nom[20];
char prenom[20];
} USER;
in this struct declaration the 20 means that each user's password, nom, prenom etc. can have a length of 19 at most, 19 because strings are NUL terminated.
The 20 in malloc(sizeof(USER)*20) means that you can have at most 20 users.
The problem is not the declaration.
The problem is that you are initializating the pointer with a runtime information.
Think about malloc as something that can only be executed when the program is actually running. Such stuff must be placed in a function.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char nom_utilisateur[20];
char mot_de_passe[20];
char nom[20];
char prenom[20];
}USER;
USER *u;
int nbr_usr=0;
void adduser() {};
void initialize(){
u = (USER *)malloc(sizeof(USER)*20);
//do all the other initializations here
}
int main(){
initialize();
return 0;
}
I am still learning C and I am doing an exercise where I have to program a car database. In the main function I declared an array of 100 pointers to 'carinfo_t' structures. In the function '*createcarinfo' a new carinfo_t instance should be created. But I get the problem that the 'brandOfCar' variable is undeclared. I do not really understand why I am getting this message because the compiler should know that this variable is part of the structure, right? The structure is declared as a datatype in the program and a pointer to the struct is initialized in the beginning of this function.
I am sorry if this question has already been asked somewhere. Any help is very much appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <limits.h>
struct carinfo_t
{
char *brandOfCar;
char *modelOfCar;
int yearCarWasBuilt;
float valueOfCar;
};
struct carinfo_t *createCarinfo(char *brand, char *model, int year, float
value)
{
struct carinfo_t *newCarInfo=(struct carinfo_t*) malloc(sizeof(struct
carinfo_t));
newCarInfo->brandOfCar=(char*)malloc(sizeof(char)*
(strlen(brandOfCar)+1));
//Message: error: 'brandOfCar' undeclared (first use in this function)
//function not finished
}
int main()
{
struct carinfo_t *carbase[100]={};
return 0;
}
This is because you called the variable passed into your constructor function brand, not brandOfCar. Similarly, you called model variable model, not modelOfCar. That's why strlen does not compile.
It's a good idea to name variables identically to the fields of the structure for consistency, and add const where it is appropriate:
struct carinfo_t *createCarinfo(
const char *brandOfCar
, const char *modelOfCar
, int yearCarWasBuilt
, float valueOfCar) {
struct carinfo_t *newCarInfo=malloc(sizeof(struct carinfo_t));
newCarInfo->brandOfCar=malloc(strlen(brandOfCar)+1);
...
}
Also note that in C you do not cast malloc, and do not multiply by sizeof(char), which standard requires to be 1 on all platforms.
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.
I have a structure dt that contains an internal array whose size is determined by argv. I need to feed this structure to a function func so I tried to declare it with a void pointer argument but the compiler is complaining that it's not being void at the end of the day. Here is the code :
#include <stdio.h>
#include <string.h>
void func(void *);
int main(int argc, char *argv[])
{
int RETSIZE;
sscanf(*++argv, "%d", &RETSIZE);
struct
{
char name[6];
char ret[RETSIZE];
} dt[100];
func(dt);
}
void func(void *dt)
{
dt->ret[2] = 3;
}
How and where do I declare the structure dt and the function func so it works out ?
If you want to use a structure in two (or more) functions, you need to define it in the global scope, so both functions have access to it. There's simply no way around it.
As for the array, in this case it's not possible to use variable-length arrays. You need to either use a flexible array member or to use a pointer. Both of these requires dynamic allocation of the heap. You also need to keep the number of elements in the array as a member in the structure.
Try This:
#include <stdio.h>
#include <string.h>
#define MAX_OBJECT 100
typedef struct
{
char name[6];
char ret[];
} dt;
void func(dt *strPtr)
{
strPtr->ret[2] = 3;
}
int main(int argc, char *argv[])
{
dt obj[MAX_OBJECT];
func(&obj[0]);
printf("Verify values written successfully or not: %d \n",obj[0].ret[2]); //Just for example
}
Here are few corrections/comments for your code:
- Keep the struct in global scope so that the struct is visible to all the whole code.
- In the func void func(void *dt), you should again type cast it the original datatype(struct in this case), in order to use it.
- As suggested in the above post, in C lang the size of the array(string) should be known to the compiler. Or else some garbage value will be present in RETSIZE and that much amount of stack memory is allocated to the ret. So, the struct should be
struct mystruct
{
char name[6];
char *ret;
};
And for memory allocation,
dt.ret = (char *)malloc(RETSIZE*sizeof(char));
- Also your func should be modifed cuz you have declared an struct array of 100, and you are passing it to the func
I am new to C.This are the files and codes that I am working on. I am trying to call a function (refineMatch) implemented in a separate file from the main function. function refineMatch returns a struct. I am having problems in compiling the code which is related to accessing elements in the returned struct. The compile error occurs in main.c file. Code below shows where the error happens.
refine.h
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
struct matchingpair{
CvPoint p1, p2;
};
struct matchingpair_array{
struct matchingpair* elements;
int length;
};
struct matchingpair_array *refineMatch(struct matchingpair* pairs,int pointcount, int bestpair);
refine.c
#include "refine.h"
#include "utils.h"
#include <stdlib.h>
struct matchingpair_array *refineMatch(struct matchingpair* pairs,int pointcount, int bestpoint){
struct matchingpair_array refinedPairs;
refinedPairs.elements=malloc(incount*sizeof(struct matchingpair));
int *in=malloc(pointcount*sizeof(int)), i=0,incount=8;
// several statements - including filling in[] with data
for(i=0;i<incount;i++){
refinedPairs.elements[i]=pairs[in[i]];
fprintf(stderr,"%d\n",in[i]);
}
refinedPairs.length=incount;
free(in);
// several other free() operations non include refinedPairs or elements
return &refinedPairs;
}
main.c
#include "refine.h"
#include <stdio.h>
int main( int argc, char** argv ){
struct matchingpair* pairs;
int matchcount=0,bestpair;
pairs=(struct matchingpair*)malloc(pairArrSize*sizeof(struct matchingpair));
//values are assigned to pairs, matchcount and bestpair
struct matcingpair_array* result=(struct matcingpair_array*)refineMatch(pairs,matchcount,bestpair); /*(casting removed this warining)
warning: initialization from incompatible pointer type*/
fprintf(stderr,"%d \n",result->length); //error: dereferencing pointer to incomplete type
//some other code
}
Please explain me what I am doing wrong here. I am using gcc.
Thank you.
refineMatch() does not return a struct. It returns a pointer to a struct matchingpair_array.
and matcingpair_array is not the same as mathcingpair_array: it is missing an h