Structures and functions in C - c

I am getting errors in the following code. The errors disappear if I take out "struct point p2...". p1 is assembled the same way and works fine, what is the catch here?
#include <stdio.h>
struct point {
int x;
int y;
};
struct point makepoint(int x, int y)
{
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
struct point addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
void main()
{
struct point p1 = makepoint(5, 7);
printf("p1 = (%d, %d)\n", p1.x, p1.y);
struct point p2 = makepoint(2, 9);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
}

#include <stdio.h>
#include <string.h>
struct arithmet {
char eval;
int first_num,sec_num;
};
/* function declaration */
void eval_value( struct arithmet *valyu );
int main( ) {
struct arithmet valyu1;
struct arithmet valyu2;
char oper;
int num1,num2;
oper=getch(); //gets(oper);
scanf("%d",&num1);
scanf("%d",&num1);
valyu1.first_num=num1;
valyu2.sec_num=num2;
valyu1.eval=oper;
eval_value(&num1);
eval_value(&num2);
eval_value(&per);
printBook( &Book1 );
printBook( &Book2 );
return 0;
}
void eval_value(struct arithmet *valyu)
{
valyu->first_num;
valyu->sec_num;
valyu->oper;
if(oper=='+')
{
printf("%d",addit(int a,int b));
}
if(oper=='-')
{
printf("%d",subtractit(int a,int b));
}
if(oper=='*')
{
printf("%d",multiplyit(int a,int b));
}
if(oper=='/')
{
printf("%d",divideit(int a,int b));
}
if(oper=='#')
{
printf("%d",intdivideit(int a,int b));
}
if(oper=='%')
{
printf("%d",remdivideit(int a,int b));
}
if(oper=='~')
{
printf("%d",exponeit(int a,int b));
}
}

#include <stdio.h>
struct arithmetic {
int x;
int y;
char oper;
};
int calcul(int a,int b)
{
struct arithmetic temp;
int x,y;
char proc;
temp.x=x;
temp.y=y;
temp.proc=proc;
if(proc=='+')
{
int addit(int x,int y)
{
return(x+y);
}
}
if(proc=='-')
{
int devit(int x,int y)
{
return(x-y);
}
}
if(proc=='*')
{
int cumlatit(int x,int y)
{
return(x*y);
}
}
if(proc=='/')
{
int dividit(int x,int y)
{
return(x/y);
}
}
if(proc=='#')
{
int intdividit(int x,int y)
{
return(x/y);
}
}
if(proc=='%')
{
int remdivit(int x,int y)
{
return(x%y);
}
}
if(proc=='`')
{
int exponit(int x,int y)
{
int i=1,k=1;
for(i=1;i<=y;i++)
{
k*=x;
}
return(k);
}
}
void main()
{
struct arithmetic v1;
int num1,num2;
char proc;
scanf(&num1);
scanf(&num1);
proc=getch();
v1.x=num1;
v1.y=num2;
v1.oper=proc;
struct calcul();
}

To avoid the many hidden calls to memcpy()
To avoid the many compiler allocated memory areas
(that cannot be accessed/used by anything else.)
I suggest the following code,
which compiles cleanly
and exercises each function
#include <stdio.h>
struct point
{
int x;
int y;
};
void makepoint(int x, int y, struct point* newPoint)
{
newPoint->x = x;
newPoint->y = y;
} // end function: makepoint
void addpoint(struct point* p1, struct point* p2)
{
p1->x += p2->x;
p1->y += p2->y;
} // end function: addpoint
int main()
{
struct point p1;
struct point p2;
makepoint(5, 7, &p1);
printf("p1 = (%d, %d)\n", p1.x, p1.y);
makepoint(2, 9, &p2);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
addpoint( &p1, &p2 );
printf("p1 = (%d, %d)\n", p1.x, p1.y);
return(0);
} // end function: main

Related

invalid operands to binary + c programming

Hi I'm finding a sum of polynomial in c, without massive, and i have this error that says " invalid operands to binary+(have 'float()(int, int, int)' and 'float()(int, int, int)'"
here is the code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float p6(int p6, int x, int a){ `function to find pow`
p6=pow(x, 6);
p6=a*p6;
return p6;
}
float p5(int p5, int x, int a){ `small function`
p5=pow(x, 5);
p5=p5*a;
return p5;
}
float p4(int p4, int x, int a){
p4=pow(x, 4);
p4=a*p4;
return p4;
}
float p3(int p3, int a, int x){
p3=pow(x, 3);
p3=a*p3;
return p3;
}
float p2(int p2, int a, int x){
p2=pow(x, 2);
p2=a*p2;
return p2;
}
main (){ `main function starts here`
int i, a;
double sum=0;
float x;
printf("x-iin utgiig oruul"); `value of x`
scanf("%lf", &x);
printf("a1-a6 toog oruul"); `value of coefficents`
for(i=1; i<=6; i++){ `for coeffincents`
scanf("%d", &a);
}
sum=p6+p5+p4+p3+p2+a*x; `error occurs here`
printf("%d", sum);
system("pause");
return 0;
}
baceause p6, p5, ... are functions so you you have to make function calls in the main. Somthing like that:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float p6(int x, int a){ //`function to find pow`
int p6=pow(x, 6);
p6=a*p6;
return p6;
}
float p5(int x, int a){ //`small function`
int p5=pow(x, 5);
p5=p5*a;
return p5;
}
float p4(int x, int a){
int p4=pow(x, 4);
p4=a*p4;
return p4;
}
float p3(int a, int x){
int p3=pow(x, 3);
p3=a*p3;
return p3;
}
float p2(int a, int x){
int p2=pow(x, 2);
p2=a*p2;
return p2;
}
int main (){ //`main function starts here`
int i, a;
double sum=0;
float x;
printf("x-iin utgiig oruul"); //`value of x`
scanf("%f", &x);
printf("a1-a6 toog oruul"); //`value of coefficents`
for(i=1; i<=6; i++){ //`for coeffincents`
scanf("%d", &a);
}
sum=p6(x, a)+p5(x, a)+p4(x, a)+p3(x, a)+p2(x, a)+a*x; //`error occurs here`
printf("%lf", sum);
system("pause");
return 0;
}

Unexpected output of function in C

With an input of an integer 4, for example, I'm trying to get the following output w/ the initializePoly function.
(0, 0)
(-1, 1)
(-2, 4)
(-3, 9)
Instead, I'm getting all 0s in the outputs' second value when I run the code. Any feedback much appreciated.
#include <stdio.h>
#include <stdlib.h>
struct point{
int x;
int y;
};
void printPoint(struct point);
void printPoly(struct point *, int);
void initializePoly(struct point *, int);
int main(void) {
// Fill in your main function here
struct point * polygon;
int num;
scanf("%d", &num);
polygon = (struct point *) malloc(num * sizeof(struct point));
initializePoly(polygon, num);
printPoly(polygon, num);
free(polygon);
}
void printPoint(struct point pt) {
printf("(%d, %d)\n", pt.x, pt.y);
}
void printPoly(struct point *ptr, int N) {
int i;
for (i=0; i<N; i++) {
printPoint(ptr[i]);
}
}
// Write your initializePoly() function here
void initializePoly(struct point *pt, int num){
int i;
for(i=0;i<num;i++)
pt[i].x = -i;
pt[i].y = i*i;
}
void initializePoly(struct point *pt, int num){
int i;
for(i=0;i<num;i++)
pt[i].x = -i;
pt[i].y = i*i;
}
same as
void initializePoly(struct point *pt, int num){
int i;
for(i=0;i<num;i++) {
pt[i].x = -i;
}
pt[i].y = i*i;
}
Use {} to iterate both assignments.
void initializePoly(struct point *pt, int num){
int i;
for(i=0;i<num;i++) {
pt[i].x = -i;
pt[i].y = i*i;
}
}

Unexpected error in testing and learning about graphs in C

I have recently started to study this particular book for algorithms and data structure SkienaTheAlgorithmDesignManual.pdf, from which I've heard a lot of praise not only on the Internet,but from my Algorithms Design teacher as well at college,and I ended up having some errors with some code used from the book on page 153(on the book itself) or 165(pdf format).
Here's the code:
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#define MAXV 1000
typedef struct {
int y;
int weight;
struct edgenode *next;
}edgenode;
typedef struct {
edgenode *edges[MAXV + 1];
int degree[MAXV + 1];
int nvertices;
int nedges;
bool directed;
}graph;
void initialize_graph(graph *g, bool directed);
void read_graph(graph *g, bool directed);
void insert_edge(graph *g, int x, int y, bool directed);
void print_graph(graph *g);
void initialize_graph(graph *g, bool directed) {
int i;
g->nvertices = 0;
g->nedges = 0;
g->directed = directed;
for (i = 1; i <= MAXV; i++) {
g->degree[i] = 0;
g->edges[i] = NULL;
}
}
void read_graph(graph *g, bool directed) {
int i;
int m;
int x, y;
initialize_graph(g, directed);
scanf("%d %d", &(g->nvertices), &m);
for (i = 1; i <= m; i++) {
scanf("%d %d", &x, &y);
insert_edge(g, x, y, directed);
}
}
void insert_edge(graph *g, int x, int y, bool directed) {
edgenode *p;
p = malloc(sizeof(edgenode));
p->weight = NULL;
p->y = y;
p->next = g->edges[x];
g->edges[x] = p;
g->degree[x]++;
if (directed == false)
insert_edge(g, y, x, true);
else
g->nedges++;
}
void print_graph(graph *g) {
int i;
edgenode *p;
for (i = 1; i <= g->nvertices; i++) {
printf("%d ", i);
p = g->edges[i];
while (p != NULL) {
printf(" %d", p->y);
p = p->next;
}
printf("\n");
}
}
main() {
bool directed = true;
graph *g;
read_graph(g, directed);
print_graph(g);
system("pause");
}
Here are the errors:
1>c:\users\dragos\source\repos\learninggraph\learninggraph\main.c(47): warning C4047: '=': 'int' differs in levels of indirection from 'void *'
1>c:\users\dragos\source\repos\learninggraph\learninggraph\main.c(49): warning C4133: '=': incompatible types - from 'edgenode *' to 'edgenode *'
1>c:\users\dragos\source\repos\learninggraph\learninggraph\main.c(65): warning C4133: '=': incompatible types - from 'edgenode *' to 'edgenode *'
1>c:\users\dragos\source\repos\learninggraph\learninggraph\main.c(73): error C4700: uninitialized local variable 'g' used
1>Done building project "LearningGraph.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I think that the main problem is the "incompatible types",but I may be as very well be wrong.
In insert_edge
p->weight = NULL;
is invalid because weight is an int but NULL a pointer (typically (void*)0)
In insert_edge
p->next = g->edges[x];
is invalid because next is the undefined type struct edgenode * but edges[x] is edgenode *. To solve that you have to replace
typedef struct {
int y;
int weight;
struct edgenode *next;
}edgenode;
by
typedef struct edgenode {
int y;
int weight;
struct edgenode *next;
}edgenode;
The reason is the same in print_graph line
p = p->next;
Explicitly set the return type of main as int
In main you call read_graph with g never set/initialized so when it is dereferenced in read_graph the behavior is undefined, and this is also the case in print_graph. Just replace
graph *g;
read_graph(g, directed);
print_graph(g);
by
graph g;
read_graph(&g, directed);
print_graph(&g);
Full modified version :
#include <stdlib.h>
#include<stdio.h>
#include<stdbool.h>
#define MAXV 1000
typedef struct edgenode {
int y;
int weight;
struct edgenode *next;
}edgenode;
typedef struct {
edgenode *edges[MAXV + 1];
int degree[MAXV + 1];
int nvertices;
int nedges;
bool directed;
}graph;
void initialize_graph(graph *g, bool directed);
void read_graph(graph *g, bool directed);
void insert_edge(graph *g, int x, int y, bool directed);
void print_graph(graph *g);
void initialize_graph(graph *g, bool directed) {
int i;
g->nvertices = 0;
g->nedges = 0;
g->directed = directed;
for (i = 1; i <= MAXV; i++) {
g->degree[i] = 0;
g->edges[i] = NULL;
}
}
void read_graph(graph *g, bool directed) {
int i;
int m;
int x, y;
initialize_graph(g, directed);
scanf("%d %d", &(g->nvertices), &m);
for (i = 1; i <= m; i++) {
scanf("%d %d", &x, &y);
insert_edge(g, x, y, directed);
}
}
void insert_edge(graph *g, int x, int y, bool directed) {
edgenode *p;
p = malloc(sizeof(edgenode));
p->weight = 0;
p->y = y;
p->next = g->edges[x];
g->edges[x] = p;
g->degree[x]++;
if (directed == false)
insert_edge(g, y, x, true);
else
g->nedges++;
}
void print_graph(graph *g) {
int i;
edgenode *p;
for (i = 1; i <= g->nvertices; i++) {
printf("%d ", i);
p = g->edges[i];
while (p != NULL) {
printf(" %d", p->y);
p = p->next;
}
printf("\n");
}
}
int main() {
bool directed = true;
graph g;
read_graph(&g, directed);
print_graph(&g);
system("pause");
}
Compilation :
pi#raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra g.c
pi#raspberrypi:/tmp $
You never allocated any memory for graph *g.
There's no need for this to be a pointer, make it a normal variable and pass its address to the functions.
int main() {
bool directed = true;
graph g;
read_graph(&g, directed);
print_graph(&g);
system("pause");
}

Using Pointers In C swapping three numbers

#include <stdio.h>
int main() {
int a, b,c;
/* Input a and b */
scanf("%d %d %d", &a, &b,&c);
while(a != -1) {
int *x = &a;
int *y = &b;
int *z = &c;
printf("Original inputs: a:%d\tb:%d\tc:%d\n", a, b,c);
reorder(a,b,c);
swap(a,b);
printf("Rearranged inputs: a:%d\tb:%d\tc:%d\n\n", a, b,c);
break;
}
}
void reorder(int *x, int *y, int *z){
if(*x > *y)
{
int temp = *x;
*x = *y;
*y = temp;
}else if(*y > *z){
int temp = *y;
*y = *z;
*z = temp;
}else if(*x > *z){
int temp = *x;
*x = *z;
*z = temp;
}
}
void swap(int *px, int *py)
{
int temp;
temp = *px;
*px = *py;
*py = temp;
}
I am new to C and learning pointers am not sure how to implement pointers to swap 3 numbers in ascending order
This might give you a way to start:
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void reorder(int* x, int* y, int* z) {
if (*x > *y) {
swap(x, y);
}
if (*y > *z) {
swap(y, z);
}
if (*z > *x) {
swap(z, x);
}
}
int main() {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
int *x = &a;
int *y = &b;
int *z = &c;
reorder(x, y, z);
}
You can use this code for your purpose :
#include <stdio.h>
void reorder(int *, int *, int *);
void swap(int *, int *);
void main()
{
int a, b, c;
printf("Enter three numbers : ");
while (scanf("%i %i %i", &a, &b, &c)==3)
{
reorder(&a, &b, &c);
printf("Now a is %d, b is %d and c is %d.\n\n", a, b, c);
printf("Enter three numbers : ");
}
}
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
void reorder(int* a, int* b, int* c) {
if (*c<*a&&*c<*b)
swap(a, c);
if (*b<*a&&*b<*c)
swap(a, b);
if (*c<*b)
swap(b, c);
}

how to make a array of pointer to call func pointer?

i have code to array of func pointer
#include <stdio.h>
int sum(int a, int b);
int subtract(int a, int b);
int mul(int a, int b);
int div(int a, int b);
int (*p[4]) (int x, int y);
int main(void)
{
int result;
int i, j, op;
p[0] = sum; /* address of sum() */
p[1] = subtract; /* address of subtract() */
p[2] = mul; /* address of mul() */
p[3] = div; /* address of div() */
printf("Enter two numbers: ");
scanf("%d %d", &i, &j);
printf("0: Add, 1: Subtract, 2: Multiply, 3: Divide\n");
do {
printf("Enter number of operation: ");
scanf("%d", &op);
} while(op<0 || op>3);
result = (*p[op]) (i, j);
printf("%d", result);
return 0;
}
int sum(int a, int b)
{
return a + b;
}
int subtract(int a, int b)
{
return a - b;
}
int mul(int a, int b)
{
return a * b;
}
int div(int a, int b)
{
if(b)
return a / b;
else
return 0;
}
code for array of pointer to function:
#include <stdio.h>
int sum(int, int);
int product(int, int);
int subtract(int, int);
int main()
{
int i = 0;
int a = 10;
int b = 5;
int result = 0;
int (*pfun[3])(int, int);
pfun[0] = sum;
pfun[1] = product;
pfun[2] = subtract;
for( i = 0 ; i < 3 ; i++)
{
result = pfun[i](a, b);
printf("\nresult = %d", result);
}
result = pfun[1](pfun[0](a, b), pfun[2](a, b));
printf("\n\nThe product of the sum and the subtract = %d\n",result);
}
int sum(int x, int y)
{
return x + y;
}
int product(int x, int y)
{
return x * y;
}
int subtract(int x, int y)
{
return x - y;
}
now how to combine this two program. such that array of pointers pointing to func pointers and the func pointers may have different number of args? any suggestion.
You not only need to store function pointers with a variable number of arguments (that is not very difficult, you could use a union for instance), but you also need to make sure you call the functions with the correct argument, and that is a bit trickier given your design.
I suggest to use a stack instead. All your functions would only take the stack as an argument:
void sum(stack_t *stack);
void subtract(stack_t *stack);
void product(stack_t *stack);
And your array could be declared this way:
typedef void callback_t(stack_t *);
callback_t *p[] =
{
sum,
subtract,
product,
/* ... */
};
Then for instance sum would be implemented as such:
void sum(stack_t *stack)
{
if (depth(stack) < 2)
perror("Not enough arguments in stack!");
int b = popstack(stack);
int a = popstack(stack);
int c = a + b;
pushstack(stack, c);
}
But unary minus would be implemented this way:
void neg(stack_t *stack)
{
if (depth(stack) < 1)
perror("Not enough arguments in stack!");
int a = popstack(stack);
pushstack(stack, -a);
}
Each function decides how many arguments they need. The caller does not need to know.

Resources