Answers for "Why do we use pointers in C?"

C
4

pointer operators in c

&  ==> address operator
*  ==> dereference operator

// Example
int a = 1;
int *ptr; // int * defines a pointer variable pointing at an int
ptr = &a; // the address of 'a' is assigned to the pointer

// 'ptr' is now equal to the address of 'a'
// when dereferenced using *, it returns the value at that address

printf("value of a: %d", *ptr); // prints "value of a: 1"
Posted by: Guest on September-10-2021
0

pointer c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *ptr, i , n1, n2;
    printf("Enter size: ");
    scanf("%d", &n1);

    ptr = (int*) malloc(n1 * sizeof(int));

    printf("Addresses of previously allocated memory: ");
    for(i = 0; i < n1; ++i)
         printf("%u\n",ptr + i);

    printf("\nEnter the new size: ");
    scanf("%d", &n2);

    // rellocating the memory
    ptr = realloc(ptr, n2 * sizeof(int));

    printf("Addresses of newly allocated memory: ");
    for(i = 0; i < n2; ++i)
         printf("%u\n", ptr + i);
  
    free(ptr);

    return 0;
}
Posted by: Guest on June-18-2021

Code answers related to "C"

Browse Popular Code Answers by Language