what about int * array and int array [] difference in C or C++?

전우경 (WooKyung Jeon) 1 Reputation point
2022-01-14T00:30:46.687+00:00

Can there be a difference if the difference between int * array and int array [] is added with a constant such as const?

I'm curious that there may be differences.

int *ar :

It emphasizes that the argument is the pointer.

It's better to use this notation when passing &i or pi from the caller.

If you look at this notation, think that you receive the bungee of an integer variable.


int ar[] :

It emphasizes that this argument is a pointer from an array.

This notation is recommended when the caller crosses the pointer constant evaluated from an array name such as arScore or arValue.

If you look at this, you can easily tell that it's an arrangement.

Developer technologies C++
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. YujianYao-MSFT 4,296 Reputation points Microsoft External Staff
    2022-01-14T02:06:47.763+00:00

    Hi @전우경 (WooKyung Jeon) ,

    what about int * array and int array [] difference in C or C++?

    The pointer occupies the memory space to store the address; the array name is the starting position of an array. The point of the pointer can be modified, but the starting position of the array in memory cannot be modified.
    Using sizeof on a pointer gives 4 bytes (32-bit) and 8 bytes (64-bit), while using sizeof on an array name gives the size of the array.
    When used as a formal parameter, int a[] and int *a have no practical difference. Of course, if you use a[] as a formal parameter, it can be more obvious that this is an array.

    Answers to your other questions:

    Using const to define an array is a kind of protection for the data in the array. When trying to modify the contents of an array of const type, an error will be generated at compile time.

    Regarding pointers and const, I suggest you read this issue.

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.