Apply for Zend Framework Certification Training

c



< Difference between malloc and calloc in c programming Structure in C >



 
Where is a pointer stored?
A pointer is stored based on how it is declared:
1. Local Pointer (inside a function)
Stored in Stack memory
void main() {
    int x = 10;
    int *p = &x;
}
x → stored in stack
p → also stored in stack
p contains the address of x
 
2. Dynamic Memory (using malloc/calloc)
Pointer itself → Stack
Data it points to → Heap
int *p;
p = (int*) malloc(sizeof(int));
p → stored in stack
Allocated memory → stored in Heap
 
3. Global Pointer
Stored in Data Segment
int *p;
Global variables (including pointers) → stored in data segment
???? Memory Visualization
STACK                HEAP
-----                -----
p → 1000 --------->  [ value stored here ]
p holds address 1000
Actual data is stored at that address
Key Points
A pointer is just a variable → stored in memory (stack/data segment)
It stores an address, not the value
The data it points to can be in stack, heap, or anywhere
???? Simple Real-Life Example
 
Think of a pointer like:
 
 A contact number (pointer)
The actual person (data)
 
The phone number is stored in your phone (memory), but it points to a real person somewhere else.
 
Basic Pointer Questions
1. What is a pointer in C?
A pointer is a variable that stores the address of another variable.
int a = 10;
int *p = &a;
p stores the address of a.
 
2. What are & and * operators?
& → Address-of operator
* → Dereference operator
int a = 5;
int *p = &a;
printf("%d", *p);  // Output: 5
 
3. What is NULL pointer?
A pointer that does not point to any valid memory location.
int *p = NULL;
 
4. What is pointer initialization?
Assigning address at declaration:
int a = 10;
int *p = &a;
 
5. What is pointer arithmetic?
Operations like increment, decrement on pointers.
int arr[] = {10,20,30};
int *p = arr;
printf("%d", *(p+1));  // Output: 20
 
6. Difference between pointer and array?
Pointer Array
Variable Collection
Can change address Fixed address
Uses dynamic memory Static
 
7. What is double pointer?
Pointer to pointer.
int a = 5;
int *p = &a;
int **q = &p;
q stores address of pointer p.
 
8. What is void pointer?
Generic pointer (can store any type address).
void *p;
int a = 10;
p = &a;
 
9. What is dangling pointer?
Pointer pointing to freed memory.
int *p = malloc(sizeof(int));
free(p);
// p is now dangling
 
10. What is wild pointer?
Uninitialized pointer.
int *p; // wild pointer
 
11. What is pointer to function?
int add(int a, int b){
    return a+b;
}
int (*fptr)(int,int) = add;
printf("%d", fptr(2,3));
 
12. What is call by value vs call by reference?
// Call by value
void fun(int x){
    x = 10;
}
// Call by reference
void fun(int *x){
    *x = 10;
}
Pointer allows modifying original value.
 
13. What is pointer to structure?
struct Student {
    int id;
};
struct Student s;
struct Student *p = &s;
p->id = 101;
 
14. What is memory leak related to pointers?
When memory is allocated but not freed.
int *p = malloc(sizeof(int));
// forgot free(p);
 
15. What is difference between malloc() and pointer?
Pointer → variable holding address
malloc() → function to allocate memory
Tricky Questions (Very Important)
 
16. Output?
int a = 10;
int *p = &a;
printf("%d %d", *p, p);
???? *p = 10, p = address
 
17. Output?
int arr[] = {1,2,3};
printf("%d", *(arr+2));
???? Output: 3
 
18. Can we increment array name?
? No
arr++; // invalid
 
19. Difference between *p++ and (*p)++
*p++ → moves pointer
(*p)++ → increments value
 
20. What is segmentation fault?
Error when accessing invalid memory.

< Difference between malloc and calloc in c programming Structure in C >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top