does anything like "void foo(int *const *p) "really exist? - c

#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()

Related

Why I cannot change the pointer (reassign it) in a function?

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;
}

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);
}

C Program, function sorting through pointers

This program is supposed to take an array, and sort it from lowest to highest value. My program won't sort any values though. I believe the error is in the selectionSort. The values i and j are present in the function, I printed them out inside the function but they are not passed into the swap function. I tried making i and j pointers but it didn't work. I just have no clue on what to do next. Any help would be appreciated.
#include <stdio.h>
#define N 5
void selectionSort(int *a, int n);
int *findLargest(int *a, int n);
void swap(int *p, int *q);
int main(void)
{
int i;
int a[N];
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
selectionSort(a, N);
printf("In sorted order:");
for (i = 0; i < N; i++) {
printf(" %d", a[i]);
}
printf("\n");
return 0;
}
void selectionSort(int *a, int n)
{
int *p = a;
int i;
int j;
if (n == 1) {
return;
}
i = *(p+n-1);
j = *findLargest(a, n);
swap(&i, &j);
selectionSort(a, n - 1);
}
int *findLargest(int *a, int n)
{
int *p;
int *p_max = a;
for(p = a + 1; p < a + n - 1; p++) {
if ( *p > *p_max)
p_max = p;
}
return p_max;
}
void swap(int *p, int *q)
{
int temp = *(p-1);
*(p-1) = *q;
*q = temp;
}
The problem is in your call of swap: you swap the content of two local variables
int i;
int j;
... // Some other code, then
swap(&i, &j);
This has no effect on the original array. You should be passing p+n-1 and findLargest(a, n) directly, or store their results in pointers, not in ints:
swap(p+n-1, findLargest(a, n));
In addition, your swap is broken: rather than swapping the content of two pointers, it assumes that p points one element past the target location. This is a bad assumption to make in a general-purpose function, such as swap, and it also leads to undefined behavior in your program.
void swap(int *p, int *q) {
int temp = *p;
*p = *q;
*q = temp;
}

Pointers and function arguments

The code snippet below gives the output 11 11 undefined value. But why am I getting the undefined value when the same statement is executed second time ? Is that anything to do with the scope of the function?
void foo(int **const p)
{
int j = 11;
*p = &j;
printf("%d ", **p);
}
int main()
{
int i = 10;
int *p = &i;
foo(&p);
printf("%d ", *p);
printf("%d ", *p);
return 0;
}
When foo() exits, *p points to a variable that is no longer in existence. Therefore you are invoking undefined behaviour.
You are pointing to an item which has been already deallocated as soon as foo function returns. This is an error, then the undefined behaviour happens.
After a variable goes out of scope, it's not automatically overwritten, its value simply remains there until some other instruction uses that memory location to store another value. In the example, the execution of the first printf overwrites the memory location pointed by p, that's why when you read it a second time, you see that its value has changed.
You could fix it as follows:
void foo(int **const p)
{
int *j = (int *)malloc(sizeof(int));
*p = j;
printf("%d ", **p);
}
int main()
{
int i = 10;
int *p = &i;
foo(&p);
printf("%d ", *p);
printf("%d ", *p);
free(p)
return 0;
}
It's not beautiful but for educational purposes it can be good.

Remove Negative numbers from array in C

I am trying to remove the negative numbers from array with the following code. Unfortunately, not getting the results. It just prints the first element over and over. Can someone please let me know where am I going wrong?
#include <stdio.h>
void removenegative(int a[],int *p, int *q);
int main()
{
int a[] = {2, 3, -5, -7, 6, 9};
int i;
int *p, *q;
p = a;
q = a+6-1;
removenegative(a, p,q);
for(i=0;i<6;i++)
{
printf("%2d", *p);
}
printf("\n");
}
void removenegative(int a[],int *p, int *q)
{
int *x;
x= &a[0];
while (p<=q)
{
if (*p>=0)
{
*x = *p;
x++;
}
p++;
}
for( ; x<=q; x++)
{
*x = -1;
}
}
for(i=0;i<6;i++)
{
printf("%2d", *p);
}
You're not changing p!
You are printing only one value:
printf("%2d", *p);
do this before for loop:
p = a;
and add p++ inside loop;
It just prints the first element over and over
Of course it does so...
for(i=0;i<6;i++)
{
printf("%2d", *p);
}
You always print *p, which is a[0]
Change:
printf("%2d", *p);
to:
printf("%2d", a[i]);
You're looping i, but not using it.
Your code is correct:
void removenegative(int a[],int *p, int *q)
{
int *x;
x= &a[0]; // let x point to the first thing in a
while (p<=q) // continue while p points to an address before q
{
if (*p>=0) // if the thing pointed to by p is greater than zero...
{
*x = *p; // copy from p to x, increment x
x++;
}
p++; // increment p
}
for( ; x<=q; x++) // while x is less than q...
{
*x = -1; // fill in -1
}
}
So x is an incrementing write pointer, while p scans the array and q acts as an end-of-array marker.
As I've been beaten to saying while typing this, your output routine is incorrect.

Resources