简体   繁体   中英

Assigning Variable to Pointer vs Assiging &variable to Pointer

I am now learning about pointers. This is the sample code from a book:

#include <stdio.h>

int main()
{
        int i;

        char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
        int int_array[5] = {1, 2, 3, 4, 5};

        char *char_ptr;
        int *int_ptr;

        char_ptr = char_array;
        int_ptr = int_array;

        for(i=0; i < 5; i++) {
                printf("[integer pointer] points to %p, which contains the integer %d\n", int_ptr, *int_ptr + 1);
                int_ptr = int_ptr + 1;
        }

        for(i=0; i <5; i++) {
                printf("[char pointer] points to %p, which contains the char '%c'\n", char_ptr, *char_ptr);
                char_ptr = char_ptr + 1;
        }
}

Q. Would char *char_ptr = char_array; change how this code functions in any way or is this the same thing as lines shown above?

Q. Other sample codes from this book also assigns &variable to a pointer, which from I understand means to store the address of a variable to that pointer, (eg char_ptr = &char_array; ). This sample just assigns the variable itself to the pointer, but this program still manages to print the memory addresses that the pointer points to. Why or why not use char_ptr = &char_array; in this context? Or does it not make a difference?

> kingvon@KingVon:~/Desktop/asm$ gcc pointertypes.c
> kingvon@KingVon:~/Desktop/asm$ ./a.out [integer pointer] points to
> 0x7ffc225227a0, which contains the integer 2 [integer pointer] points
> to 0x7ffc225227a4, which contains the integer 3 [integer pointer]
> points to 0x7ffc225227a8, which contains the integer 4 [integer
> pointer] points to 0x7ffc225227ac, which contains the integer 5
> [integer pointer] points to 0x7ffc225227b0, which contains the integer
> 6 [char pointer] points to 0x7ffc225227c3, which contains the char 'a'
> [char pointer] points to 0x7ffc225227c4, which contains the char 'b'
> [char pointer] points to 0x7ffc225227c5, which contains the char 'c'
> [char pointer] points to 0x7ffc225227c6, which contains the char 'd'
> [char pointer] points to 0x7ffc225227c7, which contains the char 'e'
> kingvon@KingVon:~/Desktop/asm$

Edit- typo

A. Yes, this is same things as lines shown above.

A. From C11 Standards#6.3.2.1p3 [emphasis added]

3 Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type ''array of type'' is converted to an expression with type ''pointer to type'' that points to the initial element of the array object and is not an lvalue. ....

So, in these statements

        char_ptr = char_array;
        int_ptr = int_array;

char_array is converted to pointer of type char * that points to the initial element of the char_array and int_array is converted to pointer of type int * that points to the initial element of the int_array .

For Q1:

Q. Would char *char_ptr = char_array; change how this code functions in any way or is this the same thing as lines shown above?

char char_array[5] = {'a', 'b', 'c', 'd', 'e'};
char *char_ptr; // declaration
char_ptr = char_array; // assignment

Ans: Both are same. In the first one(in question) char_ptr declaration and initialization are done in same line and in second one they are done separately.

For Q2:

Why or why not use char_ptr = &char_array; in this context? Or does it not make a difference?

Ans: In the code above shown char_array is an array of 5 characters, when you use its name to assign to a pointer it basically means you are assigning address of its first element, so the expressions

char_ptr = char_array; // 1. decl & init done separately
char *char_ptr = char_array; //2. decl & init done same line
char *char_ptr = &char_array[0]; //3. same as 2
char *char_ptr = &char_array; // 4. this is not the correct way will give you warning about this usage, its needs char (*)[5] ptr;

all print the address of the starting element of the array. The expressions

char *char_ptr = &char_array[0]; and char *char_ptr = &char_array; are different but they give the same address

Q. Would char *char_ptr = char_array; change how this code functions in any way or is this the same thing as lines shown above?

*char_ptr = char_array; is the same thing as the lines shown above.


Q. Other sample codes from this book also assigns &variable to a pointer, which from I understand means to store the address of a variable to that pointer, (eg char_ptr = &char_array;). This sample just assigns the variable itself to the pointer, but this program still manages to print the memory addresses that the pointer points to. Why or why not use char_ptr = &char_array; in this context? Or does it not make a difference?

What you want to do here is assigning the address of a variable (an array) to a pointer . In order to get the address of an array, you simply use the array name, so you don't need the & operator to get the address of an array. Refer to this link for more details.

The & operator is used in case you want to use a pointer to point to a variable. Refer to this article to understand more about * and & operators

char *p1;
char *p2;
char var;
char arr[5];

p1 = &var
p2 = arr;

Edit : Add another way to initialize pointer

char var;
char arr[5];

char *p1 = &var; // char *p1 = var is not correct
char *p2 = arr;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM