1

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, (e.g. 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

3
  • stackoverflow.com/questions/1461432/… Commented Jan 11, 2021 at 2:13
  • Assuming there is a typo in this statement int_ptr = int_ptr = 1; and you mean int_ptr = int_ptr + 1;. Commented Jan 11, 2021 at 2:27
  • @H.S. yes you are correct -edited Commented Jan 11, 2021 at 2:36

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

To make sure I understand: Would char_ptr[2] point to char_array[2] in this context?
After this statement, char_ptr = char_array;, the char_ptr will point to the first element of char_array i.e. element at [0] index.
2

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

1 Comment

That makes a lot more sense, so an array essentially works like a pointer that points to specifically assigned elements(addresses).. I now have a much better understanding
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?

*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, (e.g. 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;

1 Comment

So it would not be appropriate to assign a variable itself int *p1 = integer1 whereas integer1 is an int? What is shown is the sample code from the book is only appropriate when assigning an array to a pointer? Pointers kinda have my brain scrambled up lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.