Answers for "pointer c"

4

c pointers

#include<stdio.h>

/*
	'*' is the dereference operator; it will grab the value at a memory address.
    	it is also used to declare a pointer variable.
    
    '&' is the address-of operator; it will grab the memory address of a variable.
*/

int main(int argc, char *argv[]) {
	int x = 45;			// Declare integer x to be 45.
    int *int_ptr = &x;	// int_ptr now points to the value of x.  Any changes made to the value at int_ptr will also modify x.
  	
  	x = 5;				// Now, the value of x is 5.  This means that the value at int_ptr is now 5.
  	*int_ptr = 2;		// Now, the value at int_ptr is 2.  This means that x is now 0.
  	int_ptr = NULL;		// int_ptr now no longer points to anything.  Make sure you never leave a dangling pointer!
    
    return 0;
}
Posted by: Guest on June-22-2021
1

poiner in c

int* pc, c, d;
c = 5;
d = -15;

pc = &c; printf("%d", *pc); // Output: 5
pc = &d; printf("%d", *pc); // Ouptut: -15
Posted by: Guest on July-01-2020
2

poiner in c

int c, *pc;

// pc is address but c is not
pc = c; // Error

// &c is address but *pc is not
*pc = &c; // Error

// both &c and pc are addresses
pc = &c;

// both c and *pc values 
*pc = c;
Posted by: Guest on July-01-2020
5

How to use pointers in C

myvar = 25;
foo = &myvar;
bar = myvar;
Posted by: Guest on January-24-2020
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
0

poiner in c

#include <stdio.h>
int main()
{
    int var =10;
    int *p;
    p= &var;

    printf ( "Address of var is: %p", &var);
    printf ( "\nAddress of var is: %p", p);

    printf ( "\nValue of var is: %d", var);
    printf ( "\nValue of var is: %d", *p);
    printf ( "\nValue of var is: %d", *( &var));

    /* Note I have used %p for p's value as it represents an address*/
    printf( "\nValue of pointer p is: %p", p);
    printf ( "\nAddress of pointer p is: %p", &p);

    return 0;
}
Posted by: Guest on July-01-2020

Browse Popular Code Answers by Language