C pointer---function relation [duplicate] - c

This question already has answers here:
Call by reference, value, and name
(6 answers)
Closed 5 years ago.
We called the function foo(&x, &y) in the main function but then why did we call swap (x, y) inside void foo function as swap (&x, &y)?
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void foo(int*p1, int*p2) {
*p1 += *p2;
swap(p1, p2);
*p2 *= *p1;
}
int main(){
int x = 5, y = 9;
foo(&x, &y);
printf("x=%d\n", x);
printf("y=%d\n", y);
system("PAUSE");
return 0;
}

The difference is the type of the parameters you call the function with.
First case:
The type of x is int and the foo function is expecting int *.
So you have foo(&x, &y);
Second case:
The type of p1 is int * and the swap function is also expecting int *.
So you have swap(p1, p2);

Related

Which code would you prefer and why? Program to swap values using pointers [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
This is a program to swap two numbers using pointers.
#include<stdlib.h>
#include<stdio.h>
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
int main(int argc, char const *argv[])
{
int *a = (int *) malloc(sizeof(int));
int *b = (int *) malloc(sizeof(int));
*a =1;
*b =2;
printf("Nos are: %i %i\n", *a, *b);
swap(a,b);
printf("Swapped: %i %i\n", *a, *b);
free(a);
free(b);
return 0;
}
Would you prefer this or,
#include<stdlib.h>
#include<stdio.h>
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
int main(int argc, char const *argv[])
{
int a= 1;
int b= 2;
printf("Nos are: %i %i\n", a, b);
swap(&a,&b);
printf("Swapped: %i %i\n", a, b);
return 0;
}
Which one is better in terms of programming standards? or which one would you prefer? (pls give some theoretical explanation as well)
Output for both the codes are same i.e it swaps and returns 2 1
I would
prefer first case if I am swapping some heavy data, lets say some struct x, so we pass address of struct which is a fixed size depends on system.
prefer second case for normal data like int or char etc..
There is no need to allocate memory and free.
I would not prefer swap function using pointers.
This task is best done by the macros:
#include <stdio.h>
#define SWAP(a,b,type) do{type c__c__c; c__c__c = (a); (a) = (b); (b) = c__c__c;}while(0)
struct s
{
int a;
char x[100];
double w[100];
};
int main()
{
double a = 5.0, b = 6.0;
int c = 5, d = 6;
struct s x = {.a = 1},y = {.a = 2};
SWAP(a, b, double);
SWAP(c, d, int);
SWAP(x, y, struct s);
printf("%f %f\n", a, b);
printf("%d, %d\n", c, d);
printf("%d, %d\n", x.a, y.a);
return 0;
}
and the code will every efficient. You also do not have to write dozens of functions for every type.
Of course you can write "generic" swap function
void *swap(void *a, void *b, size_t size)
{
unsigned char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return a;
}

Pointer to pointer as a function parameter

I have the following code:
#include <stdio.h>
#include <stdlib.h>
int f(int x, int *py, int **ppz) {
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
int main(void) {
int c = 4;
printf("f(): %d\n", f(c, &c, &&c));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
How can I access **ppz correctly, because so I get an error message: "label 'c' used, but not defined".
An int** is a pointer to an int*. You need to create a variable of type int* to be able to pass a pointer to it somewhere. Here is what you should do:
int main(void) {
int c = 4;
int* pc = &c;
printf("f(): %d\n", f(c, pc, &pc));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
Refer #ikegami's answer for an explanation of the proper use of a pointer to a pointer.
You want to modify a variable of type int, so the parameter should be int *, not int **.
You'd use int ** if you wanted to modify a variable of type int * variable. That's not the case here.
For example,
void f(int **pp) {
*pp = malloc(10);
}
int main(void) {
int *p;
f(&p);
// ...
free(p);
}

Why my pointer code is giving me wrong output in C?

Why my pointer code is giving me wrong output ?.
Where, my actual code is :
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(&x, &y);
printf("%d %d ", x, y);
}
void fun2(int *x, int *y){
*x = 6;
*y = 7;
}
void main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d",x,y);
}
My expected output is like this : 6 7 6 7
It's giving me output like this : 6 7 50 100
Thanks.
In fun1 the expression &x is a pointer to the pointer. It's of type int **.
You should not use the address-of operator there, since x and y already are pointers.
An easy way to get the compiler to warn you about this is to declare thje function fun2 before you use it:
// Declare fun2 prototype
void fun2(int *x, int *y);
void fun1(int *x, int *y)
{
...
}
// Define fun2 implementation
void fun2(int *x, int *y)
{
...
}
Before use void fun2(int*, int*), you must declare or define it
In funxtion fun1, the line printf("%d %d ", x, y); should be printf("%d %d ", *x, *y);, for x and y are int*
In function fun1, the line fun2(&x, &y); should be fun2(x, y); for x and y are int*
The following code could work:
#include <stdio.h>
void fun2(int *x, int *y){
*x = 6;
*y = 7;
}
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(x, y);
printf("%d %d\n", *x, *y);
}
int main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d\n",x,y);
return 0;
}
Method 1. Point To Point To Int
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(&x, &y);
printf("%d %d ", *x, *y);
}
void fun2(int **x, int **y){
**x = 6;
**y = 7;
}
void main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d",x,y);
}

Beginner C programming passing pointers to function is giving unpredicted result

I am learning C as a part of my curriculum, and I am still learning the concepts of pointers. The following example gives unpredicted results for me.
#include <stdio.h>
void Calculate(int *x, int *y, int *k, int *m) {
k = *x * *y;
m = *x + *y;
}
int main() {
int *k;
int *m;
int g = 10;
int h = 11;
Calculate(&g, &h, k, m);
printf("%d\n", m);
printf("%d", k);
}
Each execution is giving me different results, like
-1155180448
0
or
253276384
0
or
591649904
0
but if I change the code to
#include <stdio.h>
void Calculate(int *x, int *y, int *k, int *m) {
*k = *x * *y;
*m = *x + *y;
}
int main() {
int k;
int m;
int g = 10;
int h = 11;
Calculate(&g, &h, &k, &m);
printf("%d\n", m);
printf("%d", k);
}
it prints the correct values like
21
110
Isn't using int *k; int *m; and passing it to a function as somefunc(k, m) the same as int k; int m; somefunc(&k, &m)?
Kindly explain what is wrong here.
Thank you.
The function should be:
void Calculate(int *x, int *y, int *k, int *m) {
*k = *x * *y;
*m = *x + *y;
}
*k means an int stored in memory being pointed to by k. You don't want to modify k, you want to modify what is stored in the space k points to.
Then you call like this:
int a, b;
int g = 10;
int h = 11;
Calculate(&g, &h, &a, &b);
You tell the Calculate function whereabouts in memory the variables a and b are which will hold the result of the calculation.
Note: I used different variable names because it is confusing to use the same name k for an int in one place, and k for an int * in another place. Another plan of course would be to use k,m in main and use pk or pm in the function (meaning "pointer to k", etc.)
Also you don't need to use pointers to pass g and h and in fact this is a bad idea, unless you are planning to change those in the function too.
If you really want to test this call Calculate(&g, &h, k, m); as in your first example, passing integers g and h and integer pointers *k and *m, first make sure that the pointers really point at something, so they can be treated likewise as addresses of integer variables by your Calculate() function, or whichever function of syntax alike.
#include <stdio.h>
#include <stdlib.h> //add this header for malloc
void Calculate(int *x, int *y, int *k, int *m) {
*k = *x * *y;
*m = *x + *y;
}
int main()
{
int *k = malloc(sizeof(int)); //initialize the pointer to point at something
int *m = malloc(sizeof(int)); //initialize the pointer to point at something
int g = 10;
int h = 11;
Calculate(&g, &h, k, m);
printf("%d\n", *m);
printf("%d\n", *k);
free(k); //free the space pointed to by pointers!
free(m);
return 0;
}
This is not a very practical solution (at least as your example is concerned), but is here only to show how you should treat your pointers as function arguments from within your caller function.

C - input 2 values, return 2 values to MAIN. swapping

I need to make a program to arrange 4 numbers in ascending order, like 9 2 8 3 to 2 3 8 9.
So i thought of the logic by using swap. I would be using if/else or switch statements but first i need to solve this problem below.
I want to input 2 integers (to compare later), then swap them and return it to main function. How do i do this. Please provide the best solution though i haven't been taught pointers, structs etc.
#include <stdio.h>
int swap(int x, int y)
{
int c;
c = x;
x = y;
y = c;
return x, y;
}
int main()
{
int a = 5, b = 7;
printf("a=%d b=%d\n", a, b);
swap(a, b);
printf("a=%d b=%d\n", x, y);
return 0;
}
I want final output as 7, 5. Thanks!
return x, y;
You can not return more than one value from a function, you can pass a pointer and then modify his content:
#include <stdio.h>
void swap(int *x, int *y)
{
int c;
c = *x;
*x = *y;
*y = c;
}
int main()
{
int a = 5, b = 7;
printf("a=%d b=%d\n", a, b);
swap(&a, &b);
printf("a=%d b=%d\n", a, b);
return 0;
}
Pass Address of variables a and b
void swap(int *x, int *y)
{
int c;
c = *x;
*x = *y;
*y = c;
}
call from main swap(&a,&b);
print a and b values in main.

Resources