I have this code:
int suma(int);
int produs(int);
struct calcul{
int suma();
int produs();
}
suma()=1+2+..n;// return S
produs()=1*2*..n;// return P
I want to call it in main with
calcul sp. How do I call function inside a struct?
If I give n a struct type n=5; the result to be sp(15,120).
Thanks!!!
I guess you want something like:
struct calcul
{
int suma(int n)
{
int result = 0;
for (int i = 1; i <= n; ++i)
result += n;
return result;
}
//... similar for produs
};
int main()
{
calcul sp;
int x = sp.suma(10);
};
int suma(int);
int produs(int);
and
struct calcul{
int suma();
int produs();
};
are two completely different sets of functions, even though they have the same name. The functions in your struct are member functions and can only be called on an instance of your struct. You would do it like so:
int main()
{
calcul x;
int a = x.suma(0);
int b = x.produs(1);
}
Related
I'm trying to change the x value in this code but I'm getting segmentation fault.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int **x;
} Type;
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x[0] = &a;
return 0;
}
if you want an array of pointers to ints
int main() {
int a = 1;
Type *type = malloc(sizeof(Type));
type->x = malloc(sizeof(int*) * 10)) ;// say we need 10
type->x[0] = &a;
return 0;
}
I have some structs as following:
typedef struct {
char debutAge[15];
char finAge[15];
} Age;
typedef struct {
char type[15];
char composants[50];
Age utilisation;
} Categorie;
typedef struct {
int code;
char nom[30];
float prix;
Categorie med;
int quantitie;
} Medicament;
#define MAX 100
typedef Medicament Pharmacie[MAX];
Pharmacie P;
int nb=0;
In my main function, I'm trying to add a new element to the Pharmacie P array, this is what I tried:
void main()
{
Medicament m = Saisir();
ajouterMedicament(P, nb, m);
afficherMedicaments();
}
int ajouterMedicament(Pharmacie *ph, int *nb, Medicament m) {
int i;
for (i = 0; i < *nb; i++) {
if (m.code == ph[i].code) {
ph[i].prix = m.prix;
ph[i].quantitie = m.quantitie;
}
return 1;
}
if (*nb < MAX) {
ph[*nb] = m;
*nb += 1;
return 1;
}
return 0;
}
But I'm getting an error on this line: mif (m.code == ph[i].code) { :
expression must have struct or union type
How can I solve this ?
You don't need to declare the first argument as Pharmacie *. Pharmacie is a typedef for an array, so you don't need to add *.
int ajouterMedicament(Pharmacie ph, int *nb, Medicament m) {
And in the call, you need to pass a pointer to nb so it can be updated:
ajouterMedicament(P, &nb, m);
In general it gets confusing when you use typedef for pointer types, and you ran into that. I recommend not doing that. See Is it a good idea to typedef pointers?
Is there a way to update struct members in a for loop, I want to avoid having to update the members one-by-one. For example:
Instead of doing this:
void Update(int vaule,struct Coolstruct *Ice)
{
int vaule;
Ice->member1=vaule;
Ice->member2=vaule;
Ice->member3=vaule;
Ice->member4=vaule;
}
I was wondering if something similar to this (pseudocode) was possible:
void Update(int vaule,struct Coolstruct *Ice)
{
int vaule;
for(int i=0;i++;i<4)
{
Ice->i =vaule
}
}
Yes, it is possible.
But you need an array of objects inside of the structure Coolstruct instead of defining multiple single objects.
Here is an example:
#include <stdio.h>
struct Coolstruct {
int b[4];
};
void Update(int vaule, struct Coolstruct *Ice)
{
for(int i=0;i<4;i++)
{
Ice->b[i] = vaule;
}
return;
}
int main(void) {
struct Coolstruct x;
int y = 25;
Update(y, &x);
for(int i = 0; i < 4; i++)
{
printf("x.b[%d] = %d\n", i, x.b[i]);
}
return 0;
}
Output:
x.b[0] = 25
x.b[1] = 25
x.b[2] = 25
x.b[3] = 25
I have to write a normal sum function and a reentrant one in C. I have to pass a int and it have to be addedd to a INIT_VALUE. In the reentrant function the main pass a int* to keep the state. How can i initialize this pointer on the first call? I have to initialize it in the fun, not in the main. Thanks
#include <stdio.h>
#ifndef INIT_VALUE
#define INIT_VALUE 0
#endif
int somma(int x){
static int val = INIT_VALUE;
val += x;
return val;
}
int somma_r(int x, int* saveptr){
// pointer initialize and sum
// return old_value ;
}
int main (){
int x;
int s;
int s_r;
int *stato;
fscanf(stdin,"%d",&x);
while(x>=0){
s = somma(x);
s_r = somma_r(x,stato);
fscanf(stdin,"%d",&x);
}
printf("%d\n",s);
printf("%d\n",s_r);
return 0;
}
With the function signature in your program (int somma_r(int x, int* saveptr)) you cannot initialize the pointer on the first call.
You probably need this (3 lines of your code modified):
...
int s = INIT_VALUE; // otherwise s will not be initialized
int s_r = INIT_VALUE; // otherwise s_r will not be initialized
int stato = INIT_VALUE; // state to be used with the somma_r function
...
s_r = somma_r(x, &stato);
...
somma_r function
int somma_r(int x, int* saveptr){
*saveptr += x;
return *saveptr;
}
Version with initialisation inside the somma_r function. This requires a modification of the signature of somma_r:
int somma_r(int x, int **saveptr){
if (*saveptr == NULL) {
*saveptr = malloc(sizeof(int));
**saveptr = INIT_VALUE;
}
**saveptr += x;
return **saveptr;
}
int main (){
int x;
int s = 0;
int s_r = 0;
int *stato = NULL;
fscanf(stdin,"%d",&x);
while(x>=0){
s = somma(x);
s_r = somma_r(x,&stato);
fscanf(stdin,"%d",&x);
}
printf("%d\n",s);
printf("%d\n",s_r);
return 0;
}
Having read the chapter about sttructures from "The C programming Language book" I tried the following code. The goal is to have an array of pointer initialized with some specific value for all its points.
#include <stdio.h>
#define MAXPOINTS 1000
struct point {
int x;
int y;
};
struct point makepoint(int x, int y);
int main(int argc, const char *argv[])
{
int i;
int number1 = 5, number2 = 10;
struct point *points[1000];
for (i=0; i< MAXPOINTS; i++) {
points[i] = &(makepoint(number1, number2));
}
}
struct point makepoint(int x, int y) {
struct point my_point;
my_point.x = x;
my_point.y = y;
return my_point;
}
The error generated after running the above code is the following:
test_something.c:18:22: error: cannot take the address of an rvalue of type 'struct point'
Why does this happen since the makepoint function does return a valid point object?
Thanks in advance,
You are returning a temporary copy of a point and take his address is not a good idea.
Try this:
struct point* makepoint(int x, int y);
int main(int argc, const char *argv[]) {
int i;
int number1 = 5, number2 = 10;
struct point* points[MAXPOINTS];
for (i=0; i< MAXPOINTS; i++)
points[i] = makepoint(number1, number2);
for (i=0; i< MAXPOINTS; i++)
free(points[i]);
return 0;
}
struct point* makepoint(int x, int y) {
struct point* my_point = malloc(sizeof(struct point));
my_point->x = x;
my_point->y = y;
return my_point;
}
Anyway, in your code:
struct point *points[10];
for (i=0; i< MAXPOINTS; i++) {
points[i] = &(makepoint(number1, number2));
}
...you have an array of 10 pointers and you're trying to assign 1000 pointers (MAXPOINTS).
You cannot take the address of a value, only of a variable. This is because values don't necessarily need to live in (addressable) memory. For example: the return value of a function is (usually) passed via a register, and you cannot take the address of a register(-variable).
You could instead change your makepoint function to take a pointer to a struct point and fill it in:
struct point makepoint(struct point * in, int x, int y){
in->x = x;
in->y = y;
return *in;
}
Note that the return value isn't strictly necessary, but kept for 'backward compatability'.