Right now I'm working on a program in C that takes 3 parameters; the address of a "first" integer, address of the "second" integer and the address in which to store the maximum of the two integers. So far I have the following basic code:
void max(int* x, int* y, int* z)
{
if (x > y) {
z = x;
}
else if (y > x){
z = y;
}
}
int main()
{
int x = 6;
int y = 4;
int z;
max(&x, &y, &z);
printf("max of %d and %d = %d\n", x, y, z);
x = 12;
y = 17;
max(&x, &y, &x);
printf("x = %d, y = %d\n", x, y);
}
When executed it outputs the following:
max of 6 and 4 = 32767
x = 12, y = 17
HOWEVER! I want it to output this instead:
max of 6 and 4 = 6
x = 17, y = 17
I'm not sure where I'm going wrong in my max function. Z should not be so huge and in the second print statement x should equal y. Any help is greatly appreciated, thanks!
As you probably already know, a pointer is a variable which contains the address in memory of another variable.
To access that memory we use The Unary operator & which gives us the address of a variable.
But accessing that memory is not all what a Pointer can do, we can with that pointer modify the value of that variable where the pointer points to.
For that we need to supply * to that pointer like this *ptr.
Let's take a look at the following program:
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
int main(void){
int a = 5;
int b = 10;
printf("A = %d\nB = %d\n\n\n",a,b);
int *pa = &a;
int *pb = &b;
*pa = 50;
*pb = 100;
printf("A = %d\nB = %d\n*PA = %d\n*PB = %d\n",a,b,*pa,*pb);
return 0;
}
The output will be:
A = 5
B = 10
A = 50
B = 100
*PA = 50
*PB = 100
As you can see A and B got new values. I hope you understand that.
when you pass things by pointer, if you want to get to the values, you need to do
(*x > *y)
Which gets the value pointed at by the pointer. (x and y are pointers, so they are going to contain memory addresses of the where the values are stored)
Needs to be:
if (*x > *y) {
*z = *x;
}
else if (*y > *x){
*z = *y;
}
You are comparing pointer addresses. You should de-reference the pointers for comparisons and assignments.
void max(int* x, int* y, int* z)
{
if (*x > *y) {
*z = *x;
}
else if (*y > *x){
*z = *y;
}
}
Related
I'm new to C and I'm getting a little tangled up with the Visual Studio Code.
I did not understand what that meant.
When I run the code in another compiler it does run.
void swap(int* x, int *y);
int exe1(int* num1Ptr, int* num2Ptr, int* num3Ptr);
int main()
{
int a = 123, b = 456 , c =4;
int* pa, * pb ,*pc;
pa = &a;
pb = &b;
pc = &c;
printf("pa = %d , pb = %d, pc = %d\n", *pa, *pb, *pc);
exe1(pa, pb, pc);
printf("pa = %d , pb = %d, pc = %d\n", *pa, *pb, *pc);
return 0;
}
void swap(int* x, int* y) {
int temp = *x;
*x = *y;
*y = *x;
}
int exe1(int* num1Ptr, int* num2Ptr, int* num3Ptr) {
if (*num2Ptr > *num1Ptr) {
swap(*num1Ptr, *num2Ptr);
}
if (*num3Ptr > *num2Ptr) {
swap(*num3Ptr, *num2Ptr);
}
if (*num3Ptr > *num1Ptr) {
swap(*num3Ptr, *num1Ptr);
}
}
Your swap routine expects two pointers to int and you pass two ints to it. Remove superfluous asterisks from the calls:
swap(*num1Ptr, *num2Ptr);
should read
swap(num1Ptr, num2Ptr);
Plus the mistake inside swap() which Kaitou points out in the answer.
"read access violation" occurs when we try to access a memory address which we don't have access to. In this case it is because of swap(*num1Ptr, *num2Ptr);
This function expects a pointer (relating to memory address) and should be called as swap(num1Ptr, num2Ptr);
*num1Ptr dereferences the pointer, it is now an integer value, not a pointer to a valid address. For example *num1Ptr can be 123 in the above code. Whatever sits at memory address 123 is probably used by system and we don't have read/write access to it. Debugger throws access violation.
The compiler should also print warnings.
The swap function needs a fix *y = temp; as noted earlier.
Unrelated to your question, exe1 compares values in this order
p1 & p2
p2 & p3
p1 & p3
If this were intended as a sort function for example, the third condition can negate the first condition. You may want to change the order of comparison as shown below:
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void exe1(int* p1, int* p2, int* p3)
{
if (*p1 < *p2) swap(p1, p2);
if (*p1 < *p3) swap(p1, p3);
if (*p2 < *p3) swap(p2, p3);
}
int main()
{
int a, b, c;
a = 123, b = 456, c = 4;
exe1(&a, &b, &c); printf("%d, %d, %d\n", a, b, c);
a = 123, b = 456, c = 4000;
exe1(&a, &b, &c); printf("%d, %d, %d\n", a, b, c);
return 0;
}
void swap(int* x, int* y) {
int temp = *x;
*x = *y;
*y = *x; // this should be *y = temp;
}
Suppose I have a pointer
int *x;
Then I need to let the pointer point to an int of value, say 42.
Should I do this:
*x = 42;
or this:
int y = 42;
x = &y;
? What is the usual practice?
After this declaration
int *x;
the pointer x either is equal to NULL (if it is declared outside any function) or has an indeterminate value. So dereferencing the pointer like
*x = 42;
invokes undefined behavior.
You can write either
int y = 42;
x = &y;
or
x = malloc( sizeof( int ) );
*x = 42;
When you do:
int *x = 42;
you assigne the pointeur x to the memory case 42, that gonna make a segmentation fault.
The right way to do what you wan't is:
int y = 42;
x = &y;
x gets the adress of y (&y)
#include <stdio.h>
int main()
{
int y=42;
int x=&y;
printf("\nY = %d is at adress X = %d \n",y,x);
}
I thought that calling function by value will never work, and I should always use call by reference, but trying this code...
// call by value
#include<stdio.h>
int Add(int a, int b)
{
int c = a + b ;
return c ;
}
int main()
{
int x = 2 , y = 4 ;
int z = Add(x,y);
printf("%d\n",z);
}
output will be: 6
it works fine in both ways (call by value & call by reference),
// call by reference
#include<stdio.h>
int Add(int* a, int* b)
{
int c = *a + *b ;
return c ;
}
int main()
{
int x = 2 , y = 4 ;
int z = Add(&x,&y);
printf("%d\n",z);
}
output will be: 6
not like the famous swap function example - when calling by value it doesn't swap -
// call by value
#include <stdio.h>
void swap(int a, int b)
{
int temp;
temp = b;
b = a;
a = temp;
}
int main()
{
int x = 1 , y = 2;
printf("x = %d , y = %d\n", x,y);
swap(x, y);
printf("after swapping\n");
printf("x = %d , y = %d\n", x,y);
return 0;
}
.. it only worked calling by reference
// call by reference
#include <stdio.h>
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
int main()
{
int x = 1 , y = 2;
printf("x = %d , y = %d\n", x,y);
swap(&x, &y);
printf("after swapping\n");
printf("x = %d , y = %d\n", x,y);
return 0;
}
So How can I judge if "calling by value" going to work or not ?!
So How can I judge that call by value method is valid or not ?!
Well, it depends on what your function is about to do.
In your above example, you only need the values of (x,y) for computing, but you never plan to change their value during your function. While call-by-reference will work in this case, it is unneccessary.
In the other (indirectly given) example you obviously want to change two variable's content (that is - swap it). You can access these variables from the main-function in your Swap-function, but how can you make the change persistent? That's only possible by call-by-reference, because you have to write the changed content into a variable that survives the function.
The following will not work:
// call by value
#include<stdio.h>
void Swap(int a, int b)
{
int c = a;
a = b;
b = c;
// from here on a, b, c will be destroyed
// therefore the change cannot be seen outside the function
}
int main()
{
int x = 2 , y = 4 ;
Swap(x,y);
printf("x: %d --- y: %d\n",x,y);
}
So as a rule to keep in mind:
If you want to make a change that's supposed to survive the function's end, use call-by-reference. If you just work with some data but do not want to (or must not) change their value, use call-by-value.
#include <stdlib.h>
#include <stdio.h>
void roll_three(int* one, int* two, int* three)
{
int x,y,z;
x = rand()%6+1;
y = rand()%6+1;
z = rand()%6+1;
one = &x;
two = &y;
three = &z;
printf("%d %d %d\n", *one,*two,*three);
}
int main()
{
int seed;
printf("Enter Seed: ");
scanf("%d", &seed);
srand(seed);
int x,y,z;
roll_three(&x,&y,&z);
printf("pai: %d %d %d\n", x,y,z);
if((x==y)&&(y==z))
printf("%d %d %d Triple!\n",x,y,z);
else if((x==y)||(y==z)||(x==z))
printf("%d %d %d Double!\n",x,y,z);
else
printf("%d %d %d\n",x,y,z);
return 0;
}
This the terminal, I type 123 for the seed. However, the printf in roll_three and the printf in main give me different output? Why *one and x are different?
The problem is here:
one = &x;
two = &y;
three = &z;
Since one, two, and three are pointers you've changed what they point to, but now they no longer point to main's x, y and z. It is similar to this...
void foo(int input) {
input = 6;
}
int num = 10;
foo(num);
printf("%d\n", num); // it will be 10, not 6.
Instead you want to change the value stored in the memory they point to. So dereference them and assign them the new value.
*one = x;
*two = y;
*three = z;
You can even eliminate the intermediate values.
*one = rand()%6+1;
*two = rand()%6+1;
*three = rand()%6+1;
In roll_three, you need to change the following:
one = &x;
two = &y;
three = &z;
To:
*one = x;
*two = y;
*three = z;
The first version just points to them to the local variables. The corrected version updates the values in the caller.
Why *one and x are different?
one = &x;
should be
*one = x;
The way you did it ( one = &x ) is wrong, because you assign pointer one to the address of local variable x which no longer exists after function roll_three.
You function should look like this:
void roll_three(int* one, int* two, int* three)
{
int x,y,z;
x = rand()%6+1;
y = rand()%6+1;
z = rand()%6+1;
*one = x;
*two = y;
*three = z;
printf("%d %d %d\n", *one,*two,*three);
}
In your roll_three function,
void roll_three(int* one, int* two, int* three)
{
int x,y,z;
x = rand()%6+1; // local variable x will have a value
y = rand()%6+1;
z = rand()%6+1;
one = &x; // variable one is assigned with local variable x's address
// so *one equals to x inside the function.
// However, variable one supposed is the variable x's address in
// the main function, but now it is changed to the local x's address,
// the main function variable x's value can't be updated as expected.
// *one = x is the way you want to go.
two = &y;
three = &z;
printf("%d %d %d\n", *one,*two,*three);
}
Complete C newb here. Trying to learn/understand pointers by messing with simple code fragments.
#include <stdio.h>
void swap(int *px, int *py)
{
int tmp;
tmp = *px;
*px = *py;
*py = tmp;
}
main()
{
int *a, *b;
*a = 1;
*b = 2;
swap(&a,&b);
printf("%d %d\n", *a, *b);
}
Why is this not valid? The code works when I remove the dereferencing operator * from main.
Conceptually, this seems like it should work. I initialize a and b as pointers which point to int 1 and int 2, respectively. I then send their addresses to swap(), which should switch what they point to.
There are a couple of problems. First, the pointers a and b are not pointing to valid memory. So the assignment of the integer values is undefined (possible crash). Secondly, the call to swap (assuming a and b are pointing to valid memory) should not include the address (it is currently sending the address of the pointer variable).
The following changes would make it work:
int a, b;
a = 1;
b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
The swap() function is OK but inside main you are taking the addresses of pointers, so you're passing int** arguments to int* parameters.
int *a, *b;
swap(&a,&b);
To fix it, replace the code in main() with :
int a = 1, b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
Pointers point to data. A pointer itself doesn't comprise memory for storage, it just points to existing memory. So when you declare int *a; , you just have a garbage pointer with no useable value, and you mustn't dereference it.
The only sensible way to use pointers is to assign them the address-of something (or the result of some allocation function):
int i;
int *a = &i; // now a points to i
Therefore, the right way to use your swap function is to pass it addresses of integers:
int i = 10;
int j = -2;
swap(&i, &j);
a and b are uninitialized pointers, dereferencing them induces undefined behavior. You want:
int main() {
int a, b;
a = 1;
b = 2;
swap(&a,&b);
printf("%d %d\n", a, b);
return 0;
}
Your method signature is wrong. You ask for two pointers to int, yet you pass in two pointers to pointers to int.
When you say, " I then send their addresses to swap(), which should switch what they point to." Are you trying to change the address values within the pointer variables in main, to switch which bit of memory they are pointing to? In that case you will need another step of redirection:
#include <stdio.h>
void swap(int **px, int **py) {
int *tmp;
tmp = *px;
*px = *py;
*py = tmp;
}
int main (void) {
int x, y; /* storage to point to */
int *a, *b;
a = &x;
b = &y;
*a = 1;
*b = 2;
printf("(*a, *b, x, y) == (%d, %d, %d, %d)\n", *a, *b, x, y);
swap(&a, &b);
printf("(*a, *b, x, y) == (%d, %d, %d, %d)\n", *a, *b, x, y);
}
$ ./a.out
(*a, *b, x, y) == (1, 2, 1, 2)
(*a, *b, x, y) == (2, 1, 1, 2)
The x & y values have not changed, but a was pointing to x and now points to y and vice versa for b.