Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Sujith V S
Sujith V S

Posted on

     

Pointer and Array in C programming.

Printing corresponding address of each element of an array.

int main() {    int numbers[5] = {1, 2, 5, 8, 3};    for (int i=0; i<5; i++){        printf("%d = %p\n", numbers[i], &numbers[i]);    }    return 0;}
Enter fullscreen modeExit fullscreen mode

Output:

1 = 0x7ffffa8cf1402 = 0x7ffffa8cf1445 = 0x7ffffa8cf1488 = 0x7ffffa8cf14c3 = 0x7ffffa8cf150
Enter fullscreen modeExit fullscreen mode

Printing array elements address without index value.

int main() {    int numbers[5] = {1, 2, 5, 8, 3};    printf("Array address: %p", numbers);    return 0;}
Enter fullscreen modeExit fullscreen mode

Output:

Array address: 0x7ffcf4fe97c0
Enter fullscreen modeExit fullscreen mode
  • The memory address of the first array element and the address of the array is the same.This is because the address of the array always points to the first element of the array.
  • Also in the above code to print the array address we have used only the name of the array instead of the& sign. This is because in most context array names are by default converted to pointers and we can directly use name of the array without& sign.

Print other array elements address without index value
printf("Array address: %p", numbers + 1); - this will print the second element.
printf("Array address: %p", numbers + 2); - this will print the third element.

Printing array element's memory address using for loop without index value.

int main() {    int numbers[5] = {1, 2, 5, 8, 3};    for (int i=0; i<5; i++){        printf("%d = %p\n", numbers[i], numbers + i);    }    return 0;}
Enter fullscreen modeExit fullscreen mode

Output:
1 = 0x7ffccb8a1d70
2 = 0x7ffccb8a1d74
5 = 0x7ffccb8a1d78
8 = 0x7ffccb8a1d7c
3 = 0x7ffccb8a1d80

Access array element using pointer.
In order to use array elements using pointers, we have to use*(numbers + i)

int main() {    int numbers[5] = {1, 2, 5, 8, 3};    for (int i=0; i<5; i++){        printf("%d = %p\n", *(numbers + i), numbers + i);    }    return 0;}
Enter fullscreen modeExit fullscreen mode

Output:

1 = 0x7fff49ac30b02 = 0x7fff49ac30b45 = 0x7fff49ac30b88 = 0x7fff49ac30bc3 = 0x7fff49ac30c0
Enter fullscreen modeExit fullscreen mode

*Change array elements using pointers *

int main() {    int numbers[5] = {1, 2, 5, 8, 3};    *numbers = 76;    *(numbers + 1) = 89;    printf("%d ", *numbers);    printf("%d", *(numbers+1));    return 0;}
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

React | Django | ..
  • Joined

More fromSujith V S

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp