Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Pointer 150206204

Shulou Source: shulou.com Published: 2022-06-01 05:07:38 10月04日 Update

# include

Int main ()

{

Int * ptr; / / declare an int pointer

Int val = 1; / / declare an int value

Ptr = & val; / / assigns a reference to the int value to the pointer

Int deref = * ptr; / / A pair of pointers take values and print what is stored in the pointer address

Printf ("deref address =% ld, value =% d\ n", ptr, deref)

}

In line 2, we declare an int pointer through the * operator. Then we declare an int variable and assign it to 1. Then we initialize our int pointer with the address of the int variable. Next, take the value of the int pointer and initialize the int pointer with the memory address of the variable. Finally, we print out the value of the variable with a content of 1.

The & val in line 6 is a reference. After the val variable declares and initializes memory, we can reference the memory address of the variable directly by using the address operator before the variable name.

In line 8, we once again use the * operator to value the pointer to directly get the data in the memory address that the pointer points to. Because the type declared by the pointer is int, the value fetched is the int value stored at the memory address that the pointer points to.

Pointers and arrays

# include

Int main ()

{

Intmyarray [4] = {1, 2, 3, 0}

Int * ptr = myarray

Printf ("ptr address =% ld, value * ptr=%d\ n", ptr,*ptr)

Ptr++

Printf ("ptr address =% ld, value * ptr=%d\ n", ptr,*ptr)

Ptr++

Printf ("ptr address =% ld, value * ptr=%d\ n", ptr,*ptr)

Ptr++

Printf ("ptr address =% ld, value * ptr=%d\ n", ptr,*ptr)

}

The array of C language represents a contiguous memory space that is used to store multiple objects of a particular type. In contrast, pointers are used to store a single memory address. Arrays and pointers are not the same structure and therefore cannot be converted to each other. The array variable points to the memory address of the first element of the array.

An array variable is a constant. You cannot assign a pointer to an array variable even if it points to the same address or to a different array. Nor can you assign one array variable to another array. However, it seems puzzling that an array variable can be assigned to a pointer. When you assign an array variable to a pointer, you actually assign the address to the first element of the array to the pointer.

Pointer and structure

# include

Struct person {

Intage

Char * name

}

Int main ()

{

Struct person first

Struct person * ptr

First.age = 21

Char * fullname = "fullname"

First.name = fullname

Ptr= & first

Printf ("age=%d, name=%s\ n", first.age, ptr- > name)

}

# include

# include

# include

# include

< string.h>

# include

< stdlib.h>

Main ()

{

Int count,*array; / * count is a counter, and array is an integer pointer, which can also be understood as pointing to the first address of an integer array * /

If ((array= (int *) malloc (10*sizeof (int) = = NULL)

{

Printf ("storage space could not be allocated successfully.")

Exit (1)

}

For (count=0;count

Tags: Pointers addresses memory arrays functions variables storage allocation pointing regions spaces dynamics elements parameters operators success no use functions contents prototypes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia MariaDB MySQL Xiaomi