I'm using structures in C and when I try to return a structure from a function it always results in gibberish when I try to print the contents of that structure in main.
Here is my code :
#include <stdio.h>
struct etudiant
{
int a;
int b;
int c;
};
typedef struct etudiant ETD;
ETD ajouter_etd()
{
ETD e;
scanf("%i%i%i", e.a, e.b, e.c);
return e;
}
void main()
{
ETD e;
e = ajouter_etd();
printf("%i%i%i", e.a, e.b, e.c);
}
You must pass the addresses of your variables to scanf as it needs to know where in memory to place the results of its conversions. This is done with the address-of operator (&).
#include <stdio.h>
typedef struct etudiant {
int a;
int b;
int c;
} ETD;
ETD ajouter_etd(void)
{
ETD e;
scanf("%i%i%i", &e.a, &e.b, &e.c);
return e;
}
int main(void)
{
ETD e;
e = ajouter_etd();
printf("%i%i%i\n", e.a, e.b, e.c);
}
Related
I am trying to implement a basic strategy pattern for understanding. I am new to programming. what am i doing wrong in the following code.
Can some one give a basic c implementation of strategy pattern.Thanks in adavance
#include <stdio.h>
#include <stdlib.h>
typedef int (*CustomerPriceStrategy)(int);
int bronzePriceStrategy(int);
int silverPriceStrategy(int);
int goldPriceStrategy(int);
struct Customer
{
const char* name;
CustomerPriceStrategy priceStrategy;
};
void placeOrder(struct Customer* customer)
{
int a;
a=customer->priceStrategy(3);
printf("%d",a);
}
int main(void) {
struct Customer *customer;
customer->name="bronze";
customer->priceStrategy=&bronzePriceStrategy;
placeOrder(customer);
return EXIT_SUCCESS;
}
int bronzePriceStrategy(int a)
{
printf(" 40+ shipping");
return (a+40);
}
int silverPriceStrategy(int a)
{
printf(" 25+ shipping");
return (a+25);
}
int goldPriceStrategy(int a)
{
/* Free shipping for gold customers. */
printf(" no shipping fee");
return a;
}
struct Customer *customer;
Is an uninialized pointer so:
customer->name="bronze";
customer->priceStrategy=&bronzePriceStrategy;
Will invoke undefined behavior.
You can replace this by:
struct Customer customer;
customer.name="bronze";
customer.priceStrategy=&bronzePriceStrategy;
placeOrder(&customer);
**> Is there a way to access a variable that come from other struct? When
i try this code,i am getting this compile error.
**
test.c: In function ‘readRecordsFromFile’:
test.c:70:18: error: expected expression before ‘kdnode’
printf(" %f\n",kdnode.data.latitude);
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
#define rec_size 112
typedef struct _Node kdnode;
typedef struct _Record record;
static void readRecordsFromFile(char *filename);
struct _Record{
int plateNumber;
long *name[32];
double area;
int population;
int density;
int popcitycenter;
long region;
double latitude;
double longtitude;
};
struct _Node
{
//kdnode left;
//kdnode right;
record data;
bool type;
double x;
double y;
int pagenumber;
};
int main(){
readRecordsFromFile("data.dat");
return 0;
}
static void readRecordsFromFile(char *filename)
{
FILE* inputFile;
inputFile = fopen(filename, "rb");
int i;
if(!inputFile) {
printf("Could not open file");
return;
}
int length,record_count;
fseek(inputFile,0,SEEK_END);
length=ftell(inputFile);
fseek(inputFile,0,SEEK_SET);
record_count = length/sizeof(record);
kdnode kd;
fread(&kd,rec_size,2,inputFile);
printf("%d",ftell(inputFile));
for (i = 0; i < record_count; i++)
{
printf(" %f\n",kdnode.data.latitude);
}
fclose(inputFile);
}
typedef struct _Node is typedefed as knode. knode represents a data type and it's not an identifier, so this
printf(" %f\n",kdnode.data.latitude);
has to be
printf(" %f\n", kd.data.latitude);
You should also check return values for functions like fread() for example.
i have 2 structs, i want to assign one struct to another, but when i print the results, it prints crap, the functions : "ver_tope" is on charge to do that , what am i doing bad?, here is the code:
#include <stdio.h>
#include <stdlib.h>
#define TAM 4
typedef struct{
char nomyap[40];
int edad;
}t_info;
typedef struct {
t_info pila [TAM];
int tope;
}t_pila;
void ver_tope(const t_pila *p, t_info *d);
int main()
{
t_pila pila;
t_info info;
//I CHARGE BOTH STRUCTS
ver_tope(&pila, &info);
return 0;
}
void ver_tope(const t_pila *p, t_info *d)
{
*d = p->pila[(p->tope)-1];
return ;
}
Try adding an initialisation for pila.tope in main() ex:
... //I CHARGE BOTH STRUCTS
pila.tope =2;
ver_tope(&pila, &info);
...
That stopped the segmentation fault...
int main()
{
t_pila pila;
t_info info;
ver_tope(&pila, &info);
return 0;
}
You have not initialized either variable. Since pila is the source of the assignment you can do the following:
int main()
{
t_pila pila = { 0 };
pila.tope = 1;
t_info info;
ver_tope(&pila, &info);
return 0;
}
Here I default initialized pila and then set its tope member to 1. I did not initialize info since ver_tope assigns to it. It would be clearer if you converted ver_tope into a function that returned t_info.
I have the following code:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int a[3];
} A;
typedef struct
{
int b;
} B;
typedef struct
{
A a;
B b;
} C;
void getC(C *c_p)
{
C c;
c.a.a[0] = 1;
c.b.b = 1;
memcpy(c_p, &c, sizeof(C));
}
int main()
{
C *c;
getC(c);
}
and it says the process returned, I really can not find the reason, I assume the memcpy part is not working, can anyone helps?
The error is in your main, where you pass an uninitialized pointer to getC. You should make it point to something, like this:
C c;
getC(&c);
or
C *c = malloc(sizeof(C));
getC(c);
...
free(c);
Moreover, you do not need to use memcpy with structs - an assignment will work as well:
void getC(C *c_p)
{
C c;
c.a.a[0] = 1;
c.b.b = 1;
*c_p = c; // No memcpy
}
Does anyone know why the code below does not work with chars? It works with ints but when I want to use char to initialize structure it fails and gives a warning like:
warning: assignment makes integer from pointer without a cast
I don't know what this warning means.
#include <stdio.h>
#include <stdlib.h>
struct complex {
int re;
int im;
char name;
};
struct complex initialize (int k, int l, char nazwa)
{
struct complex x;
x.re = k;
x.im = l;
x.name= nazwa;
return x;
}
int main ()
{
struct complex e;
struct complex f;
int a;
int b;
char o;
int c;
int d;
char p;
a=5;
b=6;
o="t";
e = initialize (a, b, o);
c=8;
d=3;
p="u";
f=initialize (c, d, p);
printf("c1 = (%d,%d)\nc2 = (%d,%d)\n name 1=%s name 2=%s\n", e.re , e.im, f.re, f.im, e.name, f.name);
return 0;
}
"u" is not a char. it is a string. a char array. you want 'u' instead. But then you will have only one-character names, and you will need to replace the %s in printf with %c.
Unless you really want a string, and if so change your char in the struct to be const char*. The same goes the function parameter:
struct complex {
int re;
int im;
const char* name;
};
struct complex initialize (int k, int l, const char* nazwa) {
...
}
const char* o;
const char* p;
Note that you can initialize variables, and structs. your code can be like this:
void print_complex(int n, struct complex c) {
printf("c %d = (%d,%d)\n", n, c.re , c.im);
printf("name=%s\n", c.name);
}
int main () {
struct complex e = { 5, 6, "t" };
struct complex f = { 8, 3, "u" };
print_complex(1, e);
print_complex(2, f);
return 0;
}