Arrays in C: Pointers vs Structures

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Array
AI Thread Summary
The discussion centers on the behavior of arrays in C, particularly regarding how they are treated as pointers. When using printf with the format specifier "%p", it outputs the memory address of the first element of the array, not the addresses of all individual elements. This is because arrays decay into pointers when passed to functions. The statement "printf("%d\n", array);" is also explored, noting that it would not yield the same result as using "%p" since it is interpreted differently. The discussion highlights that the address of an integer variable can be obtained using the "&" operator, which applies to any variable type. Additionally, the conversation touches on structures, illustrating how an entire structure can be copied in C, as shown with the static structures SAI. There is a mention of historical differences in how older C compilers handled arrays compared to current standards, but specifics on changes in the language over time are not detailed.
transgalactic
Messages
1,386
Reaction score
0
Code:
printf("%p\n", array);

i know that unlike other variables
when we want to do some operation on an array
it uses the physical address of the array

but there are many cells in the array
does it prints every one of them??

does the array always defined as pointer ??
even if we make every cell as int

printf("%d\n", array);

will it make any difference?
 
Technology news on Phys.org
printf("...", array) is actually read as:
printf("...", &array[0]);

So that prints out the memory address of the first array index.
 
&integer =the address of the variable ?
 
&variable = addres of the variable regardless if its type.
 
thanks
 
Note that structures work differently.

Code:
typedef struct {arrayofints[5];}SAI;

static SAI sai0 = {1,2,3,4,5};
static SAI sai1;

int main()
{
    sai1 = sai0;    // copies entire structure
    return(0);
}

I do recall that some older C compilers treated arrays the same as structures are handled now, but it's not part of the C language now (I don't know what if anything changed between the orignal C language and what exists today).
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
2
Views
1K
Replies
22
Views
3K
Replies
25
Views
2K
Replies
3
Views
2K
Replies
23
Views
2K
Replies
3
Views
1K
Back
Top