Search
 
SCRIPT & CODE EXAMPLE
 

C

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

pointer in c

#include <stdio.h>

int main()
{
    int j;
    int i;
    int *p; // declare pointer

    i = 7;
    p = &i; // The pointer now holds the address of i
    j = *p;

    printf("i = %d 
", i);  // value of i
    printf("j = %d 
", j);  // value of j
    printf("*p = %d 
", p); // the address held by the pointer

    *p = 32; // asigns value to i via the pointer

    printf("i = %d 
", i);   // value of i
    printf("j = %d 
", j);   // value of j
    printf("*p = %d 
", p);  // the address held by the pointer
    printf("&i = %d 
", &i); // address of i
  
  	return 0;
    }
Comment

how to make pointers in c

datatype *var;

variable var actually holds the address of the data(memory where it is stored)
*var lets you access the data stored at that address
Comment

c structure with pointer

#include<stdio.h>
 
struct Point
{
   int x, y;
};
 
int main()
{
   struct Point p1 = {1, 2};
 
   // p2 is a pointer to structure p1
   struct Point *p2 = &p1;
 
   // Accessing structure members using structure pointer
   printf("%d %d", p2->x, p2->y);
   return 0;
}
Comment

pointer in C

#include <stdio.h>
int main()
{
   int* pc, c;
   
   c = 22;
   printf("Address of c: %p
", &c);
   printf("Value of c: %d

", c);  // 22
   
   pc = &c;
   printf("Address of pointer pc: %p
", pc);
   printf("Content of pointer pc: %d

", *pc); // 22
   
   c = 11;
   printf("Address of pointer pc: %p
", pc);
   printf("Content of pointer pc: %d

", *pc); // 11
   
   *pc = 2;
   printf("Address of c: %p
", &c);
   printf("Value of c: %d

", c); // 2
   return 0;
}
Comment

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
",ptr + i);

    printf("
Enter 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
", ptr + i);
  
    free(ptr);

    return 0;
}
Comment

c pointers

// Variable declaration
type var;

// Get the address (a pointer to) a variable
// &var   ->   type*

// Pointer declaration(s)
type* ptr_a;
type* ptr_b; 

// Change what pointer is pointing to 
ptr_a = &var;
ptr_b = ptr_a;

// Can declare and assign in 1 line
type* ptr_c = &var;
type* ptr_d = ptr_c;

// "Pointers" are also variables
// &ptr_d   ->   type**
type** ptr_e = &ptr_d;

// Getting values from pointers
// *ptr_d   ->   type
// *ptr_e   ->   type*
type value = *ptr_d; 
Comment

function pointer in c

// Basic syntax
ret_type (*fun_ptr)(arg_type1, arg_type2,..., arg_typen);

// Example
void fun(int a)
{
    printf("Value of a is %d
", a);
}

// fun_ptr is a pointer to function fun() 
void (*fun_ptr)(int) = &fun;
Comment

pointers c

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

   p= &var;

   printf("Value of variable var is: %d", var);
   printf("
Value of variable var is: %d", *p);
   printf("
Address of variable var is: %p", &var);
   printf("
Address of variable var is: %p", p);
   printf("
Address of pointer p is: %p", &p);
   return 0;
}
#output
#Value of variable var is: 10
#Value of variable var is: 10
#Address of variable var is: 0x7fff5ed98c4c
#Address of variable var is: 0x7fff5ed98c4c
#Address of pointer p is: 0x7fff5ed98c50

Comment

pointer in C

#include<stdio.h>
#include<stdlib.h>
main()
{
    int *p;
    p=(int*)calloc(3*sizeof(int));
    printf("Enter first number
");
    scanf("%d",p);
    printf("Enter second number
");
    scanf("%d",p+2);
    printf("%d%d",*p,*(p+2));
    free(p);
}
Comment

PREVIOUS NEXT
Code Example
C :: Write a c program to add two numbers without using addition operator. 
C :: vbl share price 
C :: c pass two dimensional array to function 
C :: C do...while loop 
C :: function for 2d dynamic array 
C :: how to get value of multidimensional array in c 
C :: c constants 
C :: pre and post increment in c 
C :: c check if is a right triangle 
Dart :: how to diable flutter for web 
Dart :: flutter textformfield hide underline 
Dart :: text overflow ellipsis flutter 
Dart :: how to change input text color in flutter 
Dart :: flutter switch color 
Dart :: how to change color in container flutter 
Dart :: dart datetime difference 
Dart :: flutter textfield with icon onclick 
Dart :: raisedbutton shape flutter 
Dart :: flutter listtile minverticalpadding 
Dart :: fix overflow flutter 
Dart :: generate method o dart list 
Dart :: custom error snackbar flutter 
Dart :: flutter column mainaxissize 
Dart :: text field validation in flutter 
Dart :: dart typeof 
Dart :: flutter alertdialog actionsoverflowdirecation 
Dart :: flutter pretext on textfield 
Dart :: flutter back button with data 
Dart :: how to display current date time in flutter 
Dart :: flutter transform translate 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =