Arrays in C: Pointers vs Structures

  • Thread starter transgalactic
  • Start date
  • Tags
    Array
In summary, when performing operations on an array, the physical address of the array is used. This means that it will print out every cell in the array. Arrays are always defined as pointers, even if the cells are of integer type. However, structures work differently and may not follow this same rule.
  • #1
transgalactic
1,395
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
  • #2
printf("...", array) is actually read as:
printf("...", &array[0]);

So that prints out the memory address of the first array index.
 
  • #3
&integer =the address of the variable ?
 
  • #4
&variable = addres of the variable regardless if its type.
 
  • #5
thanks
 
  • #6
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).
 

1. What is the difference between a pointer and an array?

A pointer is a variable that holds the memory address of another variable, while an array is a collection of multiple variables that are stored in contiguous memory locations.

2. How do you declare a pointer and an array in C++?

To declare a pointer, you use the * symbol before the variable name, and to declare an array, you use square brackets after the variable name with the size of the array specified.

3. How are pointers and arrays related?

Arrays can be accessed using pointers, which can be used to iterate through the elements of the array. Additionally, the name of an array can be treated as a pointer to its first element.

4. Can an array be a pointer in C++?

Although the name of an array can be used as a pointer, an array itself cannot be a pointer. They are two distinct types in C++.

5. How do you pass an array to a function in C++?

You can pass an array to a function by using a pointer to the first element of the array as the parameter. This allows the function to access and modify the elements of the array.

Similar threads

  • Programming and Computer Science
Replies
2
Views
933
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
754
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
4K
Back
Top