I'm learning the pointer in the function. When I pass pointer x into update(), I found that I cannot let it point to another memory? Why does this happen? Why I cannot let the x point to another memory on heap or stack? What should I do if I wanna achieve the goal?
void update(int *x){
int *a;
a=malloc(sizeof(int));
x=a;//x is still point to original a rather than memory
// assigned by malloc()
}
int main() {
int a=4;
int *b =&a;
printf("%d ",*b);
update(b);
printf("%d ",*b);
return 0;
}
Thank you~
You're passing the value of a pointer. If you want to have that pointer modified, then you'll need to pass a pointer to the pointer. Is this what you're intending?
void update(int **x){
int *a;
a = malloc(sizeof(int));
*x = a;
}
int main() {
int a = 4;
int *b = &a;
printf("%d ", b);
update(&b);
printf("%d ", b);
return 0;
}
Related
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);
}
#include<stdio.h>
int main()
{
int i = 11;
int *p = &i;
foo(&p);
printf("%d ", *p);
}
void foo(int *const *p)
{ int j = 10;
*p = &j;
printf("%d ", **p);
}
//it showed compile time error. Can anyone please explain
int *const *p
p is a pointer to a constant pointer to int.
You can change p itself;
You cannot change *p;
You can change **p.
void foo(int *const *p)
{ int j = 10;
*p = &j; // nope
printf("%d ", **p);
}
In your code you defined the method after calling it, so you should place it before main()
I wrote the same code in PHP, the value of 5 got incremented, but
why doesn't the value get incremented in C?
int foo(int x){
x++;
}
int main( ){
int y = 5;
foo(y);
printf("value of y = %d\n", y);
}
In C passing arguments is done by value rather than by reference. That is, the number that the function is working with is unique to the one passed to and has its own space in memory. To get around this do
int foo(int* x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
You should pass the parameter by reference to function foo.
int foo(int *x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
Because x, y are local variables to their functions. The scope of the variable lies within the block of the function. So you can't change the value of those variables from outside the block.
Solution:
Either you declare those variables as global variables
int y;
int foo(){
y++;
}
int main( ){
y = 5;
foo();
printf("value of y = %d\n", y);
}
Or using reference you can do that
int foo(int *x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
Might be in PHP it is directly pass-by-reference. But C has different function calls.
If you want to increment the value in the function and it should reflect in the main function. There are two possible ways :
1.
int foo(int x)
{
return ++x; //returning the incremented value
}
2.
void foo(int *x)
{
++(*x);//this function is pass-by-reference.
}
Please let me know if there are any issue.
What you could also be doing if you want to avoid using pointers is as below
int addone(int n);
int main(void) {
int n=0;
printf("Before: %d\n", n);
n=addone(n);//assign the returned value from function to the variable 'n'
printf("After: %d\n", n);
return 0;
}
int addone(int n) {
n++;
return n;
}
I am confused in C as i am newbie. i know 1.1 is giving me maximum value and 1.2 is giving me maximum value variable address [Picture].
My question is how do i call *findmax function in main?
int * findMax(int *a,int SIZE){
int i,max=*a,address,add;
for(i=0;i<SIZE;i++){
if(max<*(a+i)){
max=*(a+i);
}
}
//printf("maxium value is %d at index %x",max,&max);
return &max;
}
The * in the function definition is not a function pointer, it's the function's return type. The findMax function returns a pointer to integer. So you would call it just like any other functions in main:
int a[] = {1,2,3,4};
int *p = findMax(a, 4);
There is another problem, in your findMax function, you returned a pointer to a local variable, the storage of the variable will be no longer available when the function returns. Use it causes undefined behavior. So you can just return the max as an integer instead, if you really need to return a pointer, you should allocate it, or return a pointer that remains valid.
For example:
int* findMax(int *a,int SIZE){
int i;
int *max = a;
for(i=0;i<SIZE;i++){
if(*max<*(a+i)){
max=a+i;
}
}
return max;
}
#include<stdio.h>
int Max;
int* FindMax(int *a,int size)
{
int i;
Max=a[0];
for(i=0;i<size;i++)
{
if(Max<=a[i])
Max=a[i];
}
return &Max;
}
int main()
{
int a[10]={10,19,9,127,45,189,47,222,90,158};
printf("Address of Max Element:%p \n",FindMax(a,10));
printf("Max of Elements:%d \n",Max);
getchar();
return 0;
}
I have to write a swap function for my bubble sort
That's what I've got:
void swap(int arr[], int size, int i, int j)
{
int temp = *(arr+i);
*(arr + i) = *(arr+j);
*(arr+j) = temp;
}
When I'm trying to run I get the following errors:
warning C4013: 'swap' undefined; assuming extern returning int
error C2371: 'swap' : redefinition; different basic types
When I do change the function to the type of int, it does work, any idea why?
I don't need a prototype because it's before the main function... do I?
Here's the whole code:
//BubbleSort
void bubbleSort(int arr[], int size)
{
int i,j;
for(i=0; i < size; i++)
{
for(j=i+1; j < size; j++)
{
if(*(arr+i) > *(arr+j))
{
/*temp = *(arr+i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;*/
swap(arr,i,j);
}
}
}
}
void swap(int arr[], int i, int j)
{
int temp = *(arr+i);
*(arr + i) = *(arr+j);
*(arr+j) = temp;
}
void main()
{
int i, arr[] = {8,0,6,-22,9};
bubbleSort(arr, sizeof(arr)/sizeof(int));
for(i=0; i < sizeof(arr)/sizeof(int); i++)
{
printf("%d, ",*(arr+i));
}
printf("\n");
}
You seem to lack a proper prototype for the function.
Add
void swap(int arr[], int size, int i, int j);
before the first call.
Also, there's really little point in using such pointer-centric notation for the indexing, especially confusing since you declared the arr argument as an array. It's cleaner to just use:
const int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
Notice use of const for the temp value too, since it's not going to change after being assigned. Not a big deal in a 3-line function, but a good habit.
You need place void swap(int arr[], int i, int j) on top of void bubbleSort() because you're using swap() inside bubbleSort().
If not you will run into the implicit declaration of C, that is, in main(), you're calling bubbleSort(), and bubbleSort() will call swap(), but at this point, bubbleSort() does not know about you declaration of swap() since it is declared below it. So, what your compiler understand is that you're calling a swap() which is implicitly declared.
And later, when your compiler comes across your real declaration of void swap(int arr[], int i, int j), it will complain that it is a redefinition.
Other than moving your swap() declaration on to the top most, you can also solve by making the function declaration at the top most, and the definition below separately.
Besides, you don't seem to use pointer the right way, because you already passed int arr[] into the swap() function, where you can do direct swapping as pointed out by #unwind:
const int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
Consider this swap():
void swap(int *x, int *y){
int temp = *x;
*x=*y;
*y=temp;
}
This is important for you to learn that you can actually change the content of a variable by passing the address into a function, like swap(int *x, int *y), it will be very convenient. Example:
int x,y;
x=1;
y=1;
increment_both_coordinate(x,y);
//after this call, you want to have x = 2, y = 2
This can only be achieve using the method similar to swap(int *x, int *y). This is just an illustration, you will understand how useful this can be when you see them in future.
Inside bubbleSort() you call a function named swap() but, at that point in the code, there is no function named swap() either defined or declared.
Solution 1: move the defintion of swap() to before the definition of bubbleSort()
Solution 2: specify the prototype of swap() before defining bubbleSort()
If the function is void, you can't return a number.
Change your
return 0;
to
return;
Because you're returning 0. Take out the return statement and you should be fine, especially since you're operating on pointers instead of copy values.
Using "void" means the function doesn't return any value,but actually your function return "0",which is an int type.So you should use int instead of void before function definition.
Here is a function for swapping integers:
void swap(int *x, int *y) {
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
int main(int argc, char* argv) {
int a,b;
a = 5;
b = 10;
swap(&a, &b);
printf("a = %d, b = %d\n", a, b);
return 0;
}
You can swap two array cells this way: swap(&arr[i], &arr[j]);