Arrays in C: Pointers vs Structures

  • Thread starter Thread starter transgalactic
  • Start date Start date
  • Tags Tags
    Array
Click For Summary

Discussion Overview

The discussion revolves around the behavior of arrays and pointers in C programming, particularly focusing on how arrays are treated in terms of memory addresses and their relationship with structures. Participants explore the implications of using arrays as pointers and the differences in handling structures.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant notes that using printf with an array prints the memory address of the first element, questioning whether it prints every cell in the array.
  • Another participant clarifies that printf with an array is interpreted as the address of the first index of the array.
  • A participant inquires about the address of an integer variable, suggesting a basic understanding of pointers.
  • Another participant states that the address operator (&) gives the address of a variable regardless of its type.
  • A later reply introduces structures, explaining that structures are handled differently than arrays, and mentions historical context regarding older C compilers.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding pointers and arrays, with some clarifications provided but no consensus reached on the broader implications of these concepts.

Contextual Notes

There are unresolved questions about the treatment of arrays in different contexts and the historical changes in the C language regarding arrays and structures.

Who May Find This Useful

Readers interested in C programming, particularly those exploring memory management, pointers, and data structures.

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).
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
1
Views
2K
Replies
47
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K