Question:
In c, what is the difference between array variable vs integer variable?
2012-11-10 02:55:01 UTC
For example, when you pass in an integer integer, you use the ampersand operator to pass on the address. But when you pass in arrays, it is considered an address right away? Why is that? I find this inconsistent in C
Four answers:
Paul
2012-11-10 03:10:14 UTC
"But when you pass in arrays, it is considered an address right away?"

That's true. The correct description is the array "decays" to a pointer. It is actually pretty unintuitive and hard to get your head around all the details.



An array is equivalent to a pointer, but an array and a pointer are not the same. It is tricky to understand.



An array and a pointer to an array are different. An array is more than a pointer to the first element. For example, an array has fixed length at compile time whereas a pointer to an array does not; an array stores how many elements it has whereas a pointer does not.



There are a ton of nuances to this that other places explain much better than I can. If you really want to understand what is going on, I strongly recommend you read these links. They are the best, most concise explaination I have found.
husoski
2012-11-10 12:49:05 UTC
Yes, it's "inconsistent", but in the same sense as Java and other languages where objects are passed by reference and primitive data types are passed by value.



There are two major reasons for passing arrays by reference (pointer) rather than by value. Memory efficiency is usually quoted as the primary reason. Indeed, some early implementations of C also passed structs by reference, presumably for that reason.



Another reason for passing arrays by reference is to allow variable length arrays as parameters. The called function has no compiled length for the array, and the array itself has no stored length, so how much of the stack is used for that array, and where does the next argument begin? If you solved the problem with a stored length, then you'd still have a calculation to do to find arguments on the stack after the array. C was designed to be simple and fast--a substitute for assembly for writing an operating system kernel.



I suspect that the second reason is more important, since structs are passed by value in all versions of C since the 1989 ANSI standard. The size of a struct is known to the called function at compile time.



There's some "why" for you. There's also a clue. If you have a fixed-size array you'd like to pass around by value instead of using pointers, wrap it in a struct. I've used that for passing game state information for a board game like checkers or Othello.
AnalProgrammer
2012-11-10 11:05:50 UTC
An int variable is a basic data type and the variable is that type and holds the data as allocated.

An array is a pointer to a list of same type variables. The elements.



Have fun.
SaSoft
2012-11-10 12:58:50 UTC
In simple word.

ARRAY: there are two types of array. A) constant Array B) variable Array.

Constant Array: like we assign " int arr[3]. in this case " arr pointing the first element of array" arr[0];

it is fixed size .



variable Array: it s mean pointer array which can be point any where. like " int *ptr; can be declare ptr=arr;

or we can increment/decrement the pointer array like ptr++;( which jump depend on data type if data type is int so it will jump up to 4 bits) it is not fixed size.





.......

integer variable :just simple int x; so we can declare with different value any where.

just like that.

int x=10;

x=55;

so now the value of x 55.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...