Pass structure array to function using pointers - c

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

Related

Struct variable doesn't work in main function in 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;
}

C segmentation fault while using structures

I'm trying to write a data structure with two elements, and then defining a variable of that type struct. However, after initializing the variable in the main function, I'm getting segmentation fault and I don't know why.
#include <stdio.h>
#include <string.h>
struct AnimalSizes {
char stringName[50];
double sizeLength;
} animalSizes[2];
int main()
{
struct AnimalSizes *snakes;
strcpy(snakes[0].stringName,"Anaconda");
snakes[0].sizeLength=3.7;
strcpy(snakes[1].stringName,"Python");
snakes[1].sizeLength= 2.4;
printf("%c", *snakes[0].stringName);
printf("%lf", snakes[0].sizeLength);
printf("%c", *snakes[1].stringName);
printf("%lf", snakes[1].sizeLength);
return 0;
}
You try to strcpy to destination where is no allocated memory. That is undefined behavior.
You should first allocate enough memory to hold two AnimalSizes instances:
struct AnimalSizes *snakes;
snakes = malloc(2 * sizeof(struct AnimalSizes));
Also, here
printf("%c", snakes[0].stringName);
you are trying to output the first character of stringName. I assume, what you rather want to do is to output whole string with %s.
You've declared a pointer to a struct AnimalSizes, and you have declared an array struct AnimalSizes[2], but you have not made the pointer point to this array:
int main()
{
struct AnimalSizes *snakes = &animalSizes[0];
...
}
Alternatively, you may choose to not declare a global variable, rather choosing to allocate memory in main:
#include <stdlib.c>
#include <stdio.h>
#include <string.h>
struct AnimalSizes {
char stringName[50];
double sizeLength;
};
int main()
{
struct AnimalSizes *snakes = (struct AnimalSizes*) malloc(2*sizeof(struct AnimalSizes));
strcpy(snakes[0].stringName,"Anaconda");
snakes[0].sizeLength=3.7;
strcpy(snakes[1].stringName,"Python");
snakes[1].sizeLength= 2.4;
printf("%c", *snakes[0].stringName);
printf("%lf", snakes[0].sizeLength);
printf("%c", *snakes[1].stringName);
printf("%lf", snakes[1].sizeLength);
free(snakes);
return 0;
}
the following proposed code:
eliminates any need for malloc() and free()
performs the desired functionality
separates the definition of the struct from any instance of the struct.
inserts some spacing between the first letter of the snake name and the 'size' of the snake, for readability
applies certain other changes to the code for 'human' readability
and now the proposed code:
#include <stdio.h>
#include <string.h>
struct AnimalSizes
{
char stringName[50];
double sizeLength;
};
int main( void )
{
struct AnimalSizes snakes[2];
strcpy(snakes[0].stringName,"Anaconda");
snakes[0].sizeLength=3.7;
strcpy(snakes[1].stringName,"Python");
snakes[1].sizeLength= 2.4;
printf("%c ", snakes[0].stringName[0]);
printf("%lf\n", snakes[0].sizeLength);
printf("%c ", snakes[1].stringName[0]);
printf("%lf\n", snakes[1].sizeLength);
return 0;
}
a run of the proposed code outputs:
A 3.700000
P 2.400000

calling a function need struct with substruct

I don't know if my title is really clear (I don't really know how to named it) but nevermind. I've got a function with a substruct in parameter. I used the struct in the main, but not in the function because of the useless data inside for this function. My program is like that :
typedef struct vidinfo_s {
vidframe_s sVid;
int id;
[...]
};
typedef struct vidframe_s {
int phyAddr[3];
char *virAddr[3];
[...]
};
int function (vidframe_s *pVid)
My question is : I need to call a function like int callVidInfo(vidinfo_s *pVid) but I don't really know how to do it with the substruct (as I named vidframe_s) so is there a way to do that or must I call my main struct in function?
Yes, there is a way. You posted very little code, but probably you are searching for smth called offsetof or containerof:
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <assert.h>
struct vidframe_s {
int unused;
};
struct vidinfo_s {
struct vidframe_s sVid;
int id;
};
int callVidInfo(struct vidinfo_s * vidinfo) {
assert(vidinfo->id == 5);
return 0;
}
int function(struct vidframe_s *pVid) {
const size_t offsetofsVid = offsetof(struct vidinfo_s, sVid);
struct vidinfo_s * const vidinfo = (struct vidinfo_s*)((char *)pVid - offsetofsVid);
return callVidInfo(vidinfo);
}
int main() {
struct vidinfo_s var = { .id = 5 };
return function(&var.sVid);
}
See what i did there? I took the offset between struct vidinfo_s and it's member called sVid. Then i subtracted the offset from pVid pointer (which should point inside a struct vidinfo_s structure), thus i was left with the pointer to struct vidinfo_s.

Passing address of structure to a function

I have declared a structure inside main(). I want to pass the structure's address to a function. It shows structure is not declared error. I know that if a structure is declared inside main() its scope is limited. But here, I am passing the address to the function. Still it shows an error that bank * unknown size. What should I do to pass the structure's address declared inside main? Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void edit_data(struct bank *acc_data)
{
int loop;
for(loop=0;loop<200;loop++)
{
*(acc_data+loop).acc_no = 1000+loop;
(acc_data+loop).name = "a1";
(acc_data+loop)->balance = 1000;
}
};
int main()
{
int loop;
struct bank
{
long acc_no;
char name[80];
int balance;
}data[200];
edit_data(data);
}
You are mixing the declaration of the variable, and the declaration of the type of the variable.
Your function should know the type, and this type is currently declared in the main part of the program. To solve it, declare the type in the global space:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct bank
{
long acc_no;
char name[80];
int balance;
};
void edit_data(struct bank *acc_data)
{
int loop;
for(loop=0;loop<200;loop++)
{
(acc_data+loop)->acc_no = 1000+loop;
strcpy( &(acc_data+loop)->name , "a1");
(acc_data+loop)->balance = 1000;
}
};
int main()
{
int loop;
struct bank data[200];
edit_data(data);
}
Please note that you had also some pointer error in your edit data function....
It's also usually better to use array instead of pointer in that case:
void edit_data(struct bank *acc_data)
{
int loop;
for(loop=0;loop<200;loop++)
{
acc_data[loop].acc_no = 1000+loop;
strcpy( acc_data[loop].name , "a1");
acc_data[loop].balance = 1000;
}
};

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