1

Fact: In ANSI C name of the array can be used as a pointer.

For:

unsigned int array_X[10];
void function(unsigned int *array);

I can call function both ways as:

function(&array_X);
function(array_X);

My question is, what represents name of the structure? Is it also pointer and can be used in same way as the array?

CK_
  • 77
  • 1
  • 1
  • 8
  • 1
    The short answer is: no. If you have a type `struct`, then passing a variable of type `struct` is not the same as passing the address of the variable of type `struct`. – lurker Aug 20 '14 at 13:52
  • 1
    `function(&array_X);` Call incompatible pointer. – BLUEPIXY Aug 20 '14 at 13:53

7 Answers7

2

I can call function both ways

Only the second way is correct. The first one passes a pointer of incorrect type, and so would be fair to say that it works by coincidence.

what represents name of the structure?

It represents the value of the structure itself. An address of the structure happens to coincide with the address of its first member, but you must take an address explicitly using the & operator.

Going back to arrays, the fact that the name of the array can be used as a pointer to its initial member does not mean that the name of the array represents that pointer alone: the name represents the whole array, but the compiler knows how to extract the pointer depending on the expression in which the array name is used ; this is called array name decay.

Quentin
  • 62,093
  • 7
  • 131
  • 191
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

First have in mind "structure" and "array" are different things!

Array is a collection of similar data types stored in contiguous memory location. But Structure is a collection of different data types stored in contiguous memory location!

If you have a structure-

struct Struct1{
// members
}Struct_X;

function(&Struct_X); // if you change any member it will be affected

function(Struct_X); // if you change any member it will not be affected

Both are different!

Try this example-

#include<stdio.h>
struct st{
        int num;
};
void print(struct st temp)
{
        printf("num= %d\n",temp.num);
}
void change(struct st temp)
{
        temp.num=20;
}
void changenum(struct st *temp)
{
        temp->num=99;
}
int main()
{
        struct st st1;
        st1.num=10;
        print(st1);
        change(st1); // it does not modify the num
        print(st1);
        changenum(&st1); // but it modifies
        print(st1);
}

Output-

root@sathish1:~/My Docs/Programs# ./a.out 
num= 10
num= 10
num= 99
Sathish
  • 3,740
  • 1
  • 17
  • 28
0

you made a mistake.

by calling

function(&array_X);

you pass a pointer to int array to the function. in c type system, the type should be

int[] *

which is totally different meaning.

structure is the same idea.

it's better to make you idea of type tree in c clear first.

Jason Hu
  • 6,239
  • 1
  • 20
  • 41
0

Only array expressions "decay" to pointers in C; struct expressions do not. If you want a pointer to a struct, you must use the & operator.

Note: the expressions array_X and &array_X will have the same value (the address of the first element of the array is the same as the address of the array itself), but the types will be different; the first will decay to unsigned int *, while the second has type unsigned int (*)[10]; these types are incompatible, and you should at least get a warning if you try to pass &array_X in place of array_X.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

maybe take a look at this question and my answer that tries to explain the difference between array, pointer to array, to pointer degraded array:
When should an array name be treated as a pointer and when does it just represent the array itself?

Community
  • 1
  • 1
vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
0

Thanks,

well name of the structure represents really value of the structure:

#include <stdio.h>
#include <string.h>

main()
{
    struct test_t{
        unsigned char first;
        unsigned char second;
    }t_name;

    t_name.first = 0x01;
    t_name.second = 0x20;


    printf("%X", t_name );  // Output: 2001
}

Talking about arrays, when I pass as a argument &array_X you are right it's not correct, but compilers usually do automatic pointer casting and some produce error or at least warning.

CK_
  • 77
  • 1
  • 1
  • 8
-1

Only the second one is correct! The "&" for the arguments is used in others programming languages, not in C.