Has the following code any practical utility apart from showing the working of pointers?
I got it while learning c in which the tutorial showings the features of pointer.
Does a C programmer use this kind of code where a int pointer has been declared first and assigned a value inside a function?
Regards..

void check(int**);
int main()
{
    int *j;
    check(&j);
}
void check(int **i){
    int a = 20;
    *i = &a;
    }

Dani AI

Generated

As already hinted, the shown pattern makes the caller hold an address that stops being valid as soon as the callee returns. That outcome is undefined behavior: the program may appear to work, crash, or silently corrupt data. is right that passing a pointer-to-pointer is a legitimate pattern — the important part is what storage the callee provides and who owns it. ’ remark about misuse is a reminder that pointer mistakes can be dangerous; however, the sample itself is simply a lifetime bug, not a useful technique.

Safer, standard alternatives:

  • Let the callee allocate on the heap and document who frees it:
    
    #include <stdlib.h>

int allocate_value(int *out) {
int
p = malloc(sizeof p);
if (!p) return -1;
p = 20;
*out = p;
return 0;
}

/ caller:
int
j = NULL;
if (allocate_value(&j) == 0) { / use j / free(j); }
*/


- Return an allocated pointer instead of using a double pointer:
```c
int *make_value(void) {
    int *p = malloc(sizeof *p);
    if (!p) return NULL;
    *p = 20;
    return p;
}
  • Have the caller provide storage (no allocation inside callee):
    void set_value(int *dst) { *dst = 20; }
    /* caller: int j; set_value(&j); */

Practical rules and troubleshooting: always initialize pointers (NULL), document ownership (who calls free), set pointers to NULL after free, and prefer the caller-allocates or return-pointer patterns for clarity. Use tools like AddressSanitizer or Valgrind and compile with warnings (-Wall -Wextra) to detect invalid accesses. Static variables can be used to extend lifetime, but they change semantics (shared state, not thread-local) and should be used sparingly.

Recommended Answers

All 4 Replies

Only if he/she likes a pointer that refers to invalid memory(int 'a' becomes invalid when the function 'check' returns).

commented: Right +0

Has the following code any practical utility apart from showing the working of pointers?

Yes, that specific technique is highly employed in viruses for code injection.

"C programmer use this kind of code where a int pointer has been declared first and assigned a value inside a function?
"

These kind of programming is employed widely in embedded systems where a memory manager gives a memory and applications use it to fill it somewhere else.

Thank you very much for your reply. Although I don't have
much knowledge on advance use of pointer but I have got
a clue.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.