Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How does this code work

+3 votes
asked Oct 30, 2021 by PlayfulRest (160 points)
#include<stdio.h>

int main()
{
int a[] = {10, 20, 30};

printf("%d", *a +1);
}

// The output is 11 but I don't get what *a is for. Why is *a 10?

1 Answer

+5 votes
answered Oct 30, 2021 by Dino Vity (740 points)
The array variable  a  when used alone, it means the memory address (i.e. pointer) of the start of the array.

It is interpreted to be a pointer to the first element of the array, and hence it is an int pointer.

*a  means dereferencing the pointer, to get the value of the int at the memory address specified by a.

So,  *a  is just the value of the first element of the array a.   i.e. a[0]

So  *a + 1  is the same as  a[0] + 1
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...