Yesterday I had a problem with a function, turned out I forgot to declare a variable of the structure type, it is clear now what was the problem. I've modified my code, and I got another error messages, however it looks like the code actually s working.
I googled the problem but I can't really fit the solutions to my code. Can somebody shows and explain me what would be the clean way of the below code?
#include <stdio.h>
/*************************************************
include
**************************************************/
//A signal structure
typedef struct SIGNAL_STRUCTURE
{
int id;
int time;
}signal_structure;
//Structure what collects all the signals
typedef struct SIGNAL_COLLECTOR
{
signal_structure EngSpeed;
signal_structure TransReqGear;
signal_structure CurrentGear;
}signal_collector;
//Function to do with the above structure
void ManipulateSignal(signal_structure * signal)
{
signal->id = 10;
signal->time = 11;
}
/*************************************************
main
**************************************************/
void fcn(signal_collector * param_signal, int len)
{
int *pointer = param_signal;
while(len--)
{
ManipulateSignal(pointer);
pointer += (sizeof(signal_structure) / sizeof(int));
}
}
int main(void)
{
signal_collector foo;
fcn(&foo, 1);
return 0;
}
You can't use a typedef name as a function argument. You need to declare a variable with that type, and pass the address of the variable.
int main(void) {
param_collector my_pc;
fcn(&my_pc);
return 0;
}
You can only pass the reference to the object, but you forgot to define this object.
int main(void)
{
param_collector param_collector;
fcn(¶m_collector);
return 0;
}
but I would give rather call the variable a distinct name (not the same as type)
Related
I want to create an array of structs based on one struct definition, and initialize each one with a different int value.
Then, I want to print this value, using a function pointer that points to a print function.
Define a struct (includes an int and a function pointer).
create an array of 10 structs of the same definition.
set different values for each one of them.
send this value for a function that is pointed to by a function
pointer that is also located in the struct
This is my code:
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 10
void Print(int num);
typedef struct print_me
{
int x;
void (*Print)(int x);
};
struct print_me my_prints[ARRAY_SIZE];
int main()
{
size_t i = 0;
for (i = 0; i < ARRAY_SIZE; ++i)
{
my_prints[i].x = i;
my_prints[i].Print(my_prints[i].x);
}
return 0;
}
void Print(int num)
{
printf("%d\n",num);
}
I'm still learning the ideas of function pointer and structs , so I'll be glad to get some tips and suggestions that will help me to understand my mistakes here.
Thanks.
For starters there is no any sense to use the typedef specifier in this declaration
typedef struct print_me
{
int x;
void (*Print)(int x);
};
without specifying a typedef name. You could write for example
typedef struct print_me
{
int x;
void (*Print)(int x);
} print_me;
In the for loop you need to initialize the data member Print with the address of the function Print. For example
for (i = 0; i < ARRAY_SIZE; ++i)
{
my_prints[i].x = i;
my_prints[i].Print = Print;
}
then in a second for loop you could call the function like
for (i = 0; i < ARRAY_SIZE; ++i)
{
my_prints[i].Print( my_prints[i].x );
}
As with usual pointers, you have to set a pointer value before you can use it. So that it points somewhere.
Add:
my_prints[i].Print = &Print;
// or, it means the same, & is optional
// my_prints[i].Print = Print;
my_prints[i].Print(my_prints[i].x); // now ok
before calling my_prints[i].Print() so that the pointer will point to function Print before calling it.
Side note with a fun fact: because of the strange C rules, dereferencing the function pointer is not needed, and you can even like "dereference" the function pointer multiple times, like (****my_prints[i].Print)(). See ex this question.
I'm trying to pass a custom type object to a function by reference, and I can't figure out what I could be doing wrong. I read How do you pass a typedef struct to a function? as well as other references and could swear I'm already doing exactly that. I cleared out everything else I'm doing and even this spartan code throws 5 errors. Help me, Stackexchange; You're my only hope!
The goal is simply to be able to alter the values in the array in the object.
#include <stdio.h>
#include <math.h>
typedef struct structure {
char byte[10];
char mod;
} complex;
void simpleInit (complex *a, char value) {//put the value in the first byte and zero the rest
a.byte[0] = value;
char i;
for (i = 1; i < 10; ++i) {
a.byte[i] = 0;
}
a.mod = 1;
}
void main () {
complex myNumber;
char value = 6;
simpleInit (myNumber, value);
}
When I attempt to run this I get this error and 4 similar:
test2.c:10:3: error: request for member ‘byte’ in something not a structure or union
a.byte[0] = value;
a is a pointer type, so you need to de-reference it to use it. Typically that's done with the arrow operator:
a->byte[i] = 0;
Since this is just an array of bytes you can also quickly "zero" it:
memset(a, 0, 10);
Though given how important 10 is in your code you should codify that in a constant or a #define.
When you pass a value by reference you need to use asterisk to access al fields of the structure, for example:
(*a).byte[0] = value;
Happily you have -> as a shortcut, so this will be:
a->byte[0] = value;
Also do not forget to call the & (address of) operator when you call simpleInit.
#include <stdio.h>
#include <math.h>
typedef struct structure
{
char byte[10];
char mod;
} complex;
void simpleInit (complex *a, char value)
{
char i;
a->byte[0] = value;
for (i = 1; i < 10; ++i) {
a->byte[i] = 0;
}
a->mod = 1;
}
int main()
{
complex myNumber;
char value = 6;
simpleInit (&myNumber, value);
}
I have learned how to use functions and structs and pointers. I want to combined them all into one. But the code that I write doesn't seem to work. The compiler tells me the test is an undeclared identifier. Here is the code:
#include <stdio.h>
#include <stdlib.h>
struct character
{
int *power;
};
void test (use_power)
int main ()
{
test (use_power)
printf("%d\n",*power);
return 0;
}
void test ()
{
int use_power = 25;
struct character a;
a.power = &use_power;
}
Your code has many mistakes it can't even compile
Multiple missing semicolons.
Implicit declaration of test() here
test (use_power)
with a missing semicolon too.
power is not declared in main().
This line
void test use_power()
does not make sense and is invalid, and also has no semicolon.
The a instance in test() defined at the end is local to test() and as such will be deallocated when test() returns. The use_power int, has exactly the same problem and trying to extract it's address from the function is useless because you can't access it after the function has returned.
I have no idea what you were trying to do, but this might be?
#include <stdio.h>
#include <stdlib.h>
struct character {
int *power;
};
/* Decalre the function here, before calling it
* or perhaps move the definition here
*/
void test(struct character *pointer);
/* ^ please */
int
main(void) /* int main() is not really a valid signature */
{
struct character instance;
test(&instance);
if (instance.power == NULL)
return -1;
printf("%d\n", *instance.power);
free(instance.power);
return 0;
}
void
test(struct character *pointer)
{
pointer->power = malloc(sizeof(*pointer->power));
if (pointer->power != NULL)
*pointer->power = 25;
}
Your code seems to be wrong. Your definition for test contains no arguments as
void test ()
{
int use_power = 25;
struct character a;
a.power = &use_power;
}
but your prototype contains one argument
void test (use_power)
which is wrongly put. First there are no semicolons; at the end of your prototype declaration, secondly by looking at your code, use_power is a variable and not a datatype so it cannot be present solely in a function declaration.
You will get an argument mismatch error.
You have used the line in main()
printf("%d\n",*power);
which is absolutely wrong. you cannot access any member of a structure without a structure variable.
And again, you have not mentioned the; after your call to the incorrect test()before this line
As you have not put your question so properly, I must figure out what you wish to achieve. I bet you want to hold the address of a integer in the pointer member of a structure and then print its value.
Below is a code snippet which will work as you desire.
#include <stdio.h>
#include <stdlib.h>
struct character
{
int *power;
};
struct character a; //define a structure variable
void test ();
int main ()
{
test ();
printf("%d\n",*(a.power)); // print the member of structure variable a
return 0;
}
void test ()
{
int use_power = 25;
a.power = &use_power;
}
example
#include <stdio.h>
struct character {
int *power;
};
void test(struct character *var);
int main (void){
struct character use_power;
int power = 5;
use_power.power = &power;
test(&use_power);
printf("%d\n", power);
return 0;
}
void test(struct character *var){
int use_power = *var->power;
*var->power = use_power * use_power;
}
I have a address to a nested struct problem that I haven't been able to solve for a while right now.
So I have a ModuleStruct that holds a sensorStruct.
No problem to pass the address to the ModuleStruct in main. But when I do want to pass the address to the inner struct "SensorStruct" so readVoltage function can store the values, It fails.
Been googling and checking StackOverflow without finding anything that works. I hope I just done something stupid so it's easy to get it going.
Will be super happy if anyone could point me in the right direction (*puufh ;) )
typedef struct
{
int32_t Voltage;
int32_t Current;
}SensorStruct;
typedef struct
{
SensorStruct SensorData;
}ModuleStruct;
int main(void)
{
ModuleStruct Module_Data;
Sensor_Collect(&Module_Data);
}
void Sensor_Collect(ModuleStruct *Module_Data)
{
/// Here is the problem, I do not know how to pass the address of the SensorStruct
ReadVoltage(Module_Data->SensorData);
}
void ReadVoltage(SensorStruct *Sensor_Data)
{
Sensor_Data->Voltage = 5;
Sensor_Data->Current = 3;
}
Code that works. Take note that Sensor_Collect function is defined after ReadVoltage function, so it knows it's type when it calls it. Always compile your code with all warnings turned on.
#include <stdio.h>
#include <stdint.h>
typedef struct {
int32_t Voltage;
int32_t Current;
} SensorStruct;
typedef struct {
SensorStruct SensorData;
} ModuleStruct;
void ReadVoltage(SensorStruct *Sensor_Data) {
Sensor_Data->Voltage = 5;
Sensor_Data->Current = 3;
}
void Sensor_Collect(ModuleStruct *Module_Data) {
ReadVoltage(&(Module_Data->SensorData));
}
int main(void) {
ModuleStruct Module_Data;
Sensor_Collect(&Module_Data);
printf("voltage: %d\n", Module_Data.SensorData.Voltage);
printf("current: %d\n", Module_Data.SensorData.Current);
return 0;
}
I've got a struct called members which contains a bunch of char arrays and integers. The struct has been declared in Header.h and defined it by "struct members pt" in source.c, inside main. From here a for-loop is being runned 5 times and adding variables to the character arrays and ints in pt[x].
Now I need to be able to access this from a function called void search(int a); (Should probably not be a void since I want it to return a value. But I'll fix that later)
What void search is supposed to do is basicly
int willReturn[10];
int b = 0;
for(int x = 0; x<a; x++)
{
if(pt[x].hasPayed == 0)
{
willReturn[b] = x;
b++;
}
}
There might be something wrong about that code, but the thing that I need to know is how I can access pt[x].hasPayed.
Any ideas?
I do not want to use any global variables.
Thank you in advance.
Below sample code might help you.
header.h
struct members {
int hasPayed;
};
main.c
#include <stdio.h>
#include <string.h>
#include "main.h"
typedef struct members MEMBERS;
void print_member(MEMBERS *pt) {
int i;
for(i =0 ; i< 10; i++)
{
printf(" %d\n",pt[i].hasPayed);
}
}
void main () {
MEMBERS pt[10];
int i;
for(i =0 ; i< 10; i++)
{
pt[i].hasPayed= i;
}
print_member(pt);
}
Instead of print_member code your search logic.
Pass a pointer to pt as a paraamter to search. e.g void search(int a, struct members *pt).
Another way if you don't want to pass pointers. Place pt in a function as a static variable.
struct members** ____get_pt(){
static struct members pt[ /* size */ ];
/*
or for dynamical size,
static struct members* pt;
*/
return &pt;
}
// Define a macro for convenience, above where you want to use 'pt'.
#define PT (*____get_pt())
Then you can use PT[x].hasPayed everywhere, without global variables.
However, this could do not improve your code ...