Why you need to enter a pointer in a variable...

Asked 2 years ago, Updated 2 years ago, 27 views

#include <stdio.h>
void main()
{
    int a[] = {10,20,30,40,50,60,70,80,90,100};
    int k,m,i,*ptr;
    k=m=0;
    ptr=a;
    for(i=0; i<10; i+=2)
    {
        k+=ptr[i];
        m+=ptr[i+1];
    }
    printf("***** result ***** \n\n");
    printf("(10+30+50+70+90)=%d\n",k); //250
    printf("(20+40+60+80+100)=%d\n",m); //300
}

I don't understand why you have to put * up there in the *ptr.

Isn't it okay to write it in ptr without giving a pointer?

Also, I'm not sure if giving * brings up the variable a[].

c

2022-09-22 21:25

2 Answers

Hello! You can do the code you uploaded in the following way!

#include <stdio.h>
void main()
{
    int a[] = {10,20,30,40,50,60,70,80,90,100};
    int k,m,i;    //*ptr;
    k=m=0;
    // // ptr=a;
    for(i=0; i<10; i+=2)
    {
        k+=a[i];
        m+=a[i+1];
    }
    printf("***** result ***** \n\n");
    printf("(10+30+50+70+90)=%d\n",k); //250
    printf("(20+40+60+80+100)=%d\n",m); //300
}

And the reason why we received the array as a pointer is that the array itself is a set of pointers.

For example, a pointer is one compartment of a train, or a powered head, and the arrangement is one complete train.

Therefore, it seems to be a code to show it because it is possible to take an array with a pointer and use it like an array.


2022-09-22 21:25

The above answer is enough, but I'm leaving additional information for those who want to know more deeply about the relationship between the pointer and the array.

int main()
{
    int a[] = {10,20,30,40,50,60,70,80,90,100};
    // The code above is implicitly converted to the code below.
    // // int a[10] =  {10,20,30,40,50,60,70,80,90,100};
    // This is because in c, c++, the array type must be fixed in size.
    // In fact, the type of variable a is 'int [10]'.

    // But the array type is less readable because it looks a little complicated.
    // So we usually convert it to a pointer type.
    // In c, c++, the array type can be implicitly cast as a pointer type.

    int* ptr = a; // If you want to store the variable in another variable, write it like this.

    // However, in this case, in fact, the length information of the array is lost by implicit casting.
    // Array types differ from pointers in that they have array sizes in themselves.

    // In other words, the variable 'a' and the variable 'ptr' have virtually the same value (the starting address of the array)
    // 'a' has information on the type itself that the array length is 10
    // The difference between 'ptr' is that it only contains information that it is an int-type address value.

    // So where do you use the array length information? That's possible
    // The example in c is very complicated to understand
    // Consider c++'s Range-based for loop.

    // In this case, the type of variable 'a' has the length information of the array, so you can traverse the array.
    for (int i : a)
    {
        i = 0;
    }

    // However, in this case, 'ptr' is a pointer type, so there is no length information and the array cannot be traversed.
    /*
    for (int i : ptr) // error
    {
        i = 0;
    }
    */

    // The reason why the code in this part of the question can work is because
    int k,m,i;
    k=m=0;
    for (i = 0; i < 10; i += 2)
    {
        k + = ptr[i]; // because [ ] here is an operator, not a type.
        m += ptr[i+1];
    }

    // The '[ ]' operator is also available in the pointer type
    // As many as the number within '[ ]' from the address stored in that pointer variable
    // Operator that returns a reference to a value at that location that has been moved through a pointer operation.

    // That is, k + = ptr[i] is the same code as k + = *(ptr + i)
    // (ptr + i) is not adding the i value to the ptr value
    // It means adding the result of (i * size of(int)) to the ptr value. (Pointer operation)
    // The reference is returned by referring to the address value of the position changed by the pointer operation through the '*' operator
    // The returned type can be viewed as int& type.

    // For reference, to receive the above variable 'a' as a reference type,
    int(&b)[10] = a; // should be written like this

    // The reference type for an array type can be the same as the array type.
    for (int i : b) // ok
    {
        i = 0;
    }

    return 0;
}


2022-09-22 21:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.