Struct variable doesn't work in main function in c - c

I'm trying to get a struct variable from another file in c. But when I do define a variable inside of other file and trying to printing this variable in main file it didn't work.
Main File
#include <stdio.h>
#include <stdlib.h>
#include "tesst.h"
int main()
{
struct tst t;
this_test(t);
printf("%s", t.string);
return 0;
}
Other File Header
#ifndef _TESST_H
#define _TESST_H
struct tst
{
char *string;
};
void this_test(struct tst t);
#endif
Other File
#include <stdio.h>
#include <stdlib.h>
#include "tesst.h"
void this_test(struct tst t)
{
t.string = "this is test";
}
When I tried to execute this program it print nothing. How can I solve this problem?

The structure passed as a parameter to the function is only a copy. To modify the structure of the main function in the this_test function, you must pass its address. It's like the scanf function where you have to pass the address of the variables you want to modify.
#include <stdio.h>
struct tst
{
char *string;
};
void this_test(struct tst *t)
{
t->string = "this is test";
}
int main()
{
struct tst t;
this_test(&t);
printf("%s", t.string);
return 0;
}

Related

using struct variable from .h file in .c

shm_fileuploader.c:
#include "sharedMemory.h"
struct filesharing_struct temp()
{
printf("Enter the name of the file. ");
scanf("%s", &fname);
}
sharedMemory.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
//void* get();
const int SIZE = 40960;
const char *name = "obaloch_filesharing";
struct filesharing_struct{
char flag;
int size;
char content;
char fname[50];
};
//struct filesharing_struct temp;
I am getting an error saying "error: ‘fname’ undeclared" and im not sure why since fname is declared in my header file. Is there anyway i can use fname in the from the .h file in the .c file?
You need to declare a variable whose type is the structure, and scan into its fname member.
Then you can return the structure from the function, to satisfy the return type.
struct filesharing_struct temp()
{
struct filesharing_struct temp;
printf("Enter the name of the file. ");
scanf("%s", &temp.fname);
return temp;
}

unable to input string into a struct

I have a header file structs.h as follows:
#include <stdio.h>
typedef struct
{
char *name;
}
student;
In the file structs.c,
#include <stdio.h>
#include "structs.h"
int main()
{
student students[3];
scanf("%s", &students[0].name);
return 0;
}
The scanf statement gives an error since it says %s is of type char* but argument is of type char**. Removing the & gives a segmentation fault.
How else would I write this? It is a array within an array of struct variables but it should be able to take input...

Pass structure array to function using pointers

I am trying to send a structure array as reference, but for some reason I cannot get it to work, as value it is able to pass it but not as reference (&)
This is my code:
#include <stdio.h>
#include <string.h>
struct mystruct {
char line[10];
};
void func(struct mystruct record[])
{
printf ("YES, there is a record like %s\n", record[0].line);
}
int main()
{
struct mystruct record[1];
strcpy(record[0].line,"TEST0");
func(record);
return 0;
}
I thought that only by calling the function func(&record) and changing the func function arguments as "struct mystruct *record[]" it was going to work... but it didn't.
Any help please.
I think you've got your pointers and references concepts mixed up.
func(&record) would pass the address of the variable record and not a reference.
passing pointers
#include <stdio.h>
#include <string.h>
struct mystruct {
char line[10];
};
void func(struct mystruct * record)
{
printf ("YES, there is a record like %s\n", record[0].line);
// OR
printf ("YES, there is a record like %s\n", record->line);
}
int main()
{
struct mystruct record[1];
strcpy(record[0].line,"TEST0");
func(record); // or func(&record[0])
return 0;
}
if you must pass a reference, try this
#include <stdio.h>
#include <string.h>
struct mystruct {
char line[10];
};
void func(struct mystruct & record)
{
printf ("YES, there is a record like %s\n", record.line);
}
int main()
{
struct mystruct record[1];
strcpy(record[0].line,"TEST0");
func(record[0]);
return 0;
}
Update
To address the comment(s) below,
references are not available in pure C, available only in C++
the 'fault' in the original code was a struct mystruct record[] should have been struct mystruct & record

Header file with a struct and function prototypes using the keyword "extern"

I'm trying to define a struct in a header file with function prototypes that take pointer to that struct as a parameter.
#ifndef _GETDATA
#define _GETDATA
struct PERSONDATA{
char name[20];
double age,mass;
};
typedef struct PERSONDATA person;
extern void getData(person *);
extern void getName(char *,int);
#endif
The getData.c file is defined as such;
#include <stdio.h>
void getData(person *ptr)
{
printf("Enter name: ");
getName(ptr->name,sizeof(ptr->name));
}
and the getName.c file is defined as:
#include <stdio.h>
void getName(char *ptrName, int varSize)
{
int i=0;
do
{
*(ptrName++) = getchar();
++i;
if(i==varSize) printf("array full, EXITING!\n");
}while(*(ptrName-1)!='\n' && i<varSize);
}
Lastly, the main function was:
#include <stdio.h>
#include "GETDATA.h"
int main(int argc, char **argv)
{
person human1;
printf("hello, world!\n\n");
getData(&human1);
return 0;
}
On compiling the program, I get the following error:
***C:/Users/Shoaib.Shoaib-PC/Google Drive/C workspace/C workspace codelite/StructPointerExample/getData.c:2:14: error: unknown type name 'person',
void getData(person *ptr)***
Could some one please help me out here, any help is greatly appreciated!
You should include the header file in ALL files using the declared types, not just in the main file.

Sample program using Function Pointer and structures.

I created a structure and wanted to assign the values to a Function Pointer of another structure. The sample code I wrote is like below. Please see what else I've missed.
#include <stdio.h>
#include <string.h>
struct PClass{
void *Funt;
}gpclass;
struct StrFu stringfunc;
struct StrFu{
int a ;
char c;
};
Initialise(){
}
main()
{
stringfunc.a = 5;
stringfunc.c = 'd';
gpclass.Funt = malloc(sizeof(struct StrFu));
gpclass.Funt = &stringfunc;
memcpy(gpclass.Funt,&stringfunc,sizeof(struct StrFu));
printf("%u %u",gpclass.Funt->a,gpclass.Funt->c);
}
There are several problems:
A function pointer is not the same as void *, in fact you cannot rely on being able to convert between them.
You shouldn't cast the return value of malloc() in C.
You shouldn't call malloc(), then overwrite the returned pointer.
You don't need to use malloc() to store a single pointer, just use a pointer.
You shouldn't use memcpy() to copy structures, just use assignment.
There are two valid main() prototypes: int main(void) and int main(int argc, char *argv[]), and you're not using either.
there is lots of problem in your code , I try to correct it ,hope it will help
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct PClass{
void *Funt;
}gpclass;
struct StrFu{
int a ;
char c;
};
struct StrFu stringfunc;
int main()
{
stringfunc.a = 5;
stringfunc.c = 'd';
gpclass.Funt = malloc(sizeof(struct StrFu));
gpclass.Funt = &stringfunc;
memcpy(gpclass.Funt,&stringfunc,sizeof(struct StrFu));
printf("%d %c",((struct StrFu*)gpclass.Funt)->a,((struct StrFu*)gpclass.Funt)->c);
return 0;
}
it outputs
5 d

Resources