Lyrk
Lyrk

Reputation: 2000

What exactly is a variable name in C?

The value of an array name is the address of the start of (zeroth element) the array; therefore, the & is not necessary.

Ex:

scanf("%s", string1);

Then what is the value of the integer or character variable names which we have to use & operator? Not addresses like array names I hope. What are they? This is confusing.

Ex:

scanf("%d", &number1);  

What is number1 exactly? Is it an address too?

Upvotes: 0

Views: 252

Answers (5)

10111
10111

Reputation: 47

See , If you declared it like:

int number1;

Then &number1 is basically the location which will store the contents of number1.

so, when you write :

scanf("%d"&number1);

The compiler stores whatever number you input into the location specified by &number1.So,'&number1' is somewhat the address where the contents of number1 will stored.

Next time you want to use the contents of number1,they will be picked up from the memory location specified by &number.

I'd like to mention this example:

int a,b,c;
scanf("%d %d ",&a,&b);/*&a and &b are just locations assigned to a and b, in which we store integers*/
c=a+b;

At,c=a+b,here a and b will represent the integers you stored in the memory locations assigned to them ,but c will represent the memory location assigned to c,NOTICE:that there is no &operator infront of c,we just use & operator in function like scanf which are input/output functions defined in stdio.h .

When you declare int number1;

number1 can represent an address too,for eg: when you write number1=4; here number1 represent the memory location . because you are storing 4 into it.

Upvotes: 1

user529758
user529758

Reputation:

The value of an array name is the address of the start of(zeroth element) the array

No, it isn't. It's just that arrays can decay into a pointer to their first element in certain conditions (when passed to a function or assigned to a pointer, for example).

therefore, the & is not necessary. !!!!!!!

Writing 7 exclamation marks is not necessary either. Note that if arr is an array, then arr and &arr are not the same at all (as you migh assume). arr is of type T [N] where T is the type of an element in the array, and N is the number of elements. The type of &arr, however, is a "pointer-to-array-of N items of type T" (T (*)[N]). This difference is important (not only) because pointer arithmetic will yield in different results on a pointer-to-T and on a pointer-to-array-of-T.

Then what is the value of the integer or character variable names which we have to use & operator? Not addresses like array names I hope.

No, not like arrays (because they are not arrays), but & still is the addressof operator. &variable gives a pointer to the variable, of which the type is T *, if the type of variable was T.

In this specific case, you claimed that number1 was an int, so &number1 is an expression of type int *.

Upvotes: 4

Darshan Rivka Whittle
Darshan Rivka Whittle

Reputation: 34031

What is number1 exactly? Is it an address too?

Based on your type modifier, number1 is presumably an int, but of course we don't know that for sure because you're not telling us. If I understand correctly what you're asking, number1 is an integer and &number1 is a pointer to that integer.

Then what is the value of the integer or character variable names?

The value of an unmodified variable name is the value of that variable. Arrays are a special case, as you noted. Array names are not actually variables. Given:

int a[10];
int *pa;

you can legally do this:

pa = a;

but not this:

a = pa;

a is the name of an array, not a variable.

Update: For those who think it's weird to say that an array name is not a variable, I was practically quoting the second edition of The C Programming Language by Kernighan and Ritchie. (Ritchie was the creator of C.) Now I'll quote them directly, from page 99 of the second edition:

A pointer is a variable, so pa=a and pa++ are legal. But an array name is not a variable; constructions like a=pa and a++ are illegal.

Upvotes: 1

spamsink
spamsink

Reputation: 324

If you declare number1 and give it some value:

int number1;
number1 = 1234;

then &number1 is the address of the memory location where the variable is stored. If you peek into that memory location, you will find it holds 1234, the value of the variable.

If array_of_numbers is declared as:

int array_of_numbers[100];

then the first number in the array is array_of_numbers[0], the address of the first element in the array is &array_of_numbers[0].

array_of_numbers is exactly the same thing as &array_of_numbers[0].

the scanf("%d", &number1); expects address(es) where to put the variable(s), if you did

number1=1234;
scanf("%d", number1);

the scanf function will get whatever number it parses for %d, and try to stuff it into memory location 1234, which is not what you normally want.

Upvotes: 1

Daniel Santos
Daniel Santos

Reputation: 3295

Oh my, you do indeed have a lot to learn about C. But perhaps the confusion is mostly about arrays & pointers. For practical purposes, an array variable is just a pointer. Realistically, it's not -- its type is indeed an array, but it is implicitly converted to a pointer of the type it is an array of. I hope that helps.

Upvotes: 1

Related Questions