passing structure between function - c

I just wrote a small program to play with structures.This program works fine but i just have small doubt in one statement.Can anyone clarify me please?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct mystr
{
int a;
float b;
char a1[10];
};
void fun(struct mystr *ptr1)
{
struct mystr *ptr;
ptr=malloc(sizeof(struct mystr));
ptr->a=10;
ptr->b=1662.3456;
strcpy(ptr->a1,"xxxxxx");
*ptr1=*ptr; /* <<<<<<<<<<<- This assignment is fine? */
free(ptr);
}
void main()
{
struct mystr var1;
memset(&var1,0,sizeof(struct mystr));
fun(&var1);
printf("my data is %4d,%10.3f,%5s\n",var1.a,var1.b,var1.a1);
}
I know that i can just pass a pointer to the fun and print it free it. But i just wanted this program to be this way(passing structure variable address and filling it).
Thanks in advance.

This assignment
*ptr1=*ptr; /* <<<<<<<<<<<- This assignment is fine? */
is fine. Only there is no sense to allocate dynamically one more structure that to initialize the original structure. You could write the function simpler
void fun(struct mystr *ptr1)
{
ptr1->a = 1 0;
ptr1->b = 1 662.3456;
strcpy( ptr1->a1, "xxxxxx" );
}
Also instead of using memset after the structure object definition
struct mystr var1;
memset(&var1,0,sizeof(struct mystr));
you could write simply
struct mystr var1 = { 0 };
Take into account that function main in C shall be declared like
int main( void )
At least it shall have return type int.

in the posted code, this line:
*ptr1=*ptr;
is nonsense.
It does not copy the contents of the two structs.
to copy the contents, use memcpy( pDestination, pSource, numBytesToCopy );
I.E.
memcpy( ptr1, ptr, sizeof( struct mystr ) );

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.

Unable to assign values in a structure

I have declared the structure:
typedef struct keyValuePairs{
char* type;
char* key;
char* valueType;
char* value;
} keyValueType;
keyValueType keyValuePairs[1000];
And in a function declared the local variables as:
char key[500];
char value[500];
to hold the key value pair values as:
key[i]="abc";
value[i]="xyz"
I have assigned these local variables to the global variable as:
keyValuepairs[1].key=key.
Once i come out of the function, the values assigned in the structure is getting lost. Can someone explain where I am going wrong?
Please note I am fairly new to C.
Both key and value variables are pointers to arrays that are allocated in the stack when you are in the function. After keyValuepairs[1].key=key the global variable points to that same place in the stack. After exiting the function, the memory where those arrays were is reused.
I suggest you read up on static vs dynamic allocation in C
If I understand you correctly, you are trying something along the lines of:
typedef struct
{
char* val;
} A;
A alist[10];
void foo()
{
char t[10];
t = "abc";
alist[0].val = t;
}
int main()
{
foo();
}
First of all, the line
t = "abc";
is syntactically incorrect. You have to use something like:
strcpy(t, "abc");
But the most important error is that when you return from foo, alist[0].val points to an address that is not good any more. To make sure alist[0].val points to a valid address, you have to allocate memory from the heap for it and copy the contents of t to it.
void foo()
{
char t[10];
strcpy(t,"abc");
alist[0].val = malloc(strlen(t)+1);
strcpy(alist[0].val, t);
}
To do a thorough job, you'll have to make sure that you call free on that allocated memory at some point before you return from main.
You have not allocated memory for type, key, valueType and value.
Try static memory allocation :
typedef struct keyValuePairs{
char* type[n];
char* key[n];
char* valueType[n];
char* value[n];
}
Where n is a defined constant

C void Pointer on mainframe

I'm currently working on modifying a dump program, but I can't figure out how to properly navigate with a void pointer. Below is the function that I'm working in, and the instruction that I'm trying to execute. I've tried casting mem to a struct, but I'm not sure of the sytnax and I keep getting an error. For the code below, the specific error I'm getting is:
47 | mem = mem->tcbtio
===========> .........a..............................................
*=ERROR===========> a - CCN3122 Expecting pointer to struct or union.
Here is my function:
void hexdump(void *mem, unsigned int len)
{
mem = mem->tcbtio;
...
}
Here are my struct defintions:
struct psa {
char psastuff[540];
struct tcb *psatold;
char filler[4];
struct ascb *psaaold;
};
struct tcb {
struct prb *tcbrb;
char tcbstuff[8];
struct tiot *tcbtio;
};
struct tiot {
char tiocnjob[8];
char tiocpstn[8];
char tiocjstn[8];
};
I need to keep it as a void pointer, as I need to cast it to char and int later on in the function.
It seems as you are expecting to find a tcb struct, starting at the address pointed by mem, but the aim of the code is obscure and the question not clear.
If this is really the case, you can try this:
mem = ((struct tcb *)mem)->tcbtio;
You cannot dereference a void pointer. You can think it this way if you have a void pointer, how will compiler know what type of address it is holding. And by doing mem = mem->tcbtio how much offset it has to make.
Modify your function as:
void hexdump(void *mem, unsigned int len)
{
struct tcbtio *mem2;
mem2 = ((struct tcb*) mem) -> tcbtio;
...
// Use mem2 later
}

Setting a struct pointer to equal the address of another struct

I'm trying to set ptr to point at the first element in an array of structs so that when I go back to my main function, I can mess with it (theres reasons why I can't use vArray[0] in the main).
With this code though, its only allowing me to access the structs members in the alg function. Once its return back to main, all of its elements are now null. (I'm thinking it has something to do with a pass-by-value/pass-by-reference problem). Any way to fix this?
void alg(struct vars v[], struct vars *ptr)
{
ptr = &vars[0];
printf("%s", ptr->value); //this works here
}
int main()
{
struct vars vArray[100]; //this has been filled earlier in the code
struct vars *ptr;
alg(vArray, ptr);
printf("%s", ptr->value); //but now this returns null here
}
You need "Need of Pointer to pointer" to reflect change in function alg() in main function.
Do it as:
void alg(struct vars v[], struct vars **ptr)
{
(*ptr) = &vars[0];
printf("%s", (*ptr)->value); //this works here
}
And call function main as: alg(vArray, &ptr);
Note: both ptr in main and ptr in function alg() are two different variable.
Btw for learning purpose it is good to write function otherwise you can simply do it in one line as: struct vars *ptr = vArray;. Note array name decays into address of first element in this expression.
You have to read about pointer to pointer.
Check this code:
#include <stdio.h>
#include <stdlib.h>
struct vars {
int data;
};
void alg(struct vars *v, struct vars **ptr);
int main()
{
struct vars *ptr;
struct vars vArray;
vArray.data = 10;
alg(&vArray, &ptr);
printf("main : %d\n", ptr->data);
}
void alg(struct vars *v, struct vars **ptr)
{
*ptr = v;
printf("fun : %d\n", (*ptr)->data);
}
output :
fun : 10
main : 10
In short use double pointer it will solve your problem

C Strings in Structs

I want my struct to carry a string. I defined it like so:
typedef struct myStruct {
char* stringy
} myStruct
and a function
free(char *stringy){
//structObj is a struct object
structObj->stringy = stringy
}
Is this correct? I have a feeling that since it's a local variable, stringy will be lost, and the pointer will point to garbage.
Sorry, new with C.
It would be garbage if you were somehow using char** stringy, but structObj->stringy = stringy means "you know the thing that stringy points to? Now structObj->stringy points to that". Of course, it is still possible to unset the value which the pointer is pointing to, and at that point dereferencing will yield garbage.
Here's an example to make it clearer:
#include<stdio.h>
typedef struct mStruct {
char* stringy;
} myStruct;
myStruct * structObj;
void doSomething(char* stringy)
{
structObj->stringy = stringy;
}
int main(int argc, char* argv)
{
char* a = "abc\n";
structObj = malloc(sizeof(myStruct));
doSomething(a);
a = "qxr\n";
printf(structObj->stringy);
}// prints "abc\n"
If stringy is defined in callers of free function, as long as they keep the actual string in its place (where stringy points), no problem.
There is not any local variable declaration in your code.
You have to declare:
typedef struct myStruct {
char* stringy
} myStruct;
free(char *stringy){
myStruct *structObj;
structObj->stringy = stringy;
}
Pay attention to the semicolon that I've added to the end of the typedef declaration.
This was not not in your code.
The object structObj is a struct whose type is myStruct.
Now, your parameter stringy comes from another site, it is not lost.
But the struct structObj will have duration only inside your "free" function.
EDIT
I have fixed an error: the right declaration has to be "pointer to structObj", which is done in this way:
myStruct *structObj;
Observe that now myStruct is a non-initialized pointer, so the following assignment is legal:
structObj->stringy = stringy;
but will not work.
However I think this goes beyond the scope of the original question...
myStruct is type which you defined for your struct myStruct .that to you need to create an object before using.
you need to do like this:
typedef struct myStruct {
char *stringy;
} myStruct_t; //user defined data type
myStruct_t *obj;
// you need to allocate memory dynamically.
obj= (myStruct_t *) malloc(sizeof(myStruct_t));
usage:
scanf("%s",obj->stringy);
printf("%s",obj->stringy);
in function:
my_free(char *str) //str is local string
{
obj->stringy=str;
}
your can also try this code :
typedef struct myStruct {
char stringy[20]; //char *stringy
} myStruct_t; //user defined data type
myStruct_t obj; //object creation
usage:
scanf("%s",obj.stringy);
printf("%s",obj.stringy);
in function:
my_free(char *str) //str is local string
{
strcpy(obj.stringy,str);
}
You're correct that as soon as what it points to goes out of scope, it will point to garbage: this is a dangling pointer. You'll need to allocate some memory and perform a copy to fix this:
add_string(my_struct* s, const char* c)
{
size_t len = strlen(c);
s->file = malloc(len + 1);
strcpy(s->file, c);
}
Don't forget that you'll need to free it when you're done:
void destroy_struct(my_struct* s)
{
free(s->file);
free(s);
}

Resources