Difference between Structure and Array in C

«
»
Question: What is Basic Difference Between an Array and a Structure in C?

Answer: Though arrays and structures are composite data structures, they differ in various ways. An array is a sequence of elements of same type in consecutive memory locations. For example,

    int balloons[10];
    float rainfall[N];

‘balloons’ is an array of 10 integers in consecutive locations. An element of type ‘int’ on Linux takes 4 bytes of storage. Thus, ‘balloons’ is allocated 40 bytes of storage and name ‘balloons’ refers to the beginning of this block; more precisely to the first element. Further, name of an array is a pointer constant that is, for example,

    int shots = 10;
    int * const pi = &shots;    /* 'pi' is a pointer constant */

‘pi’ is a pointer constant keeps pointing to ‘shots’. Likewise, ‘balloons’ can’t be made to point elsewhere, because ‘balloons’ is a fixed address in memory. Expression such as,

    balloons += 1;

isn’t valid. Since, ‘balloons’, an array of integers, is a pointer constant or briefly a pointer or address, it can be passed as an argument to a function without need to copy the entire array to function argument. Thus, it allows functions to manipulate on the original values of array elements and not on the copy. For example,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
#include <stdio.h>
void disp(const int *, const int);
 
int main(void)
{
    int balloons[5] = {1,2,3,4,5};
 
    disp(balloons, 5);
    return 0;
}
 
void disp(const int *sb, const int count)
{
    int i = 0;
 
    while (i < count)
        printf("balloons[%d] has %d balloons.\n", i++, *(sb + i));
}

Notice that in function disp() we used ‘const’ keyword to prevent function from modifying contents of the array accidentally.

Now, discuss what a structure is? A structure is a data structure that can contain multiple different types of elements. For example,

Check this: BCA MCQs | C Books
/* structure declaration */
struct NEW {
            int count;
            char ch;
            float avg;
            struct NEW *ptr;
    };

The above structure declaration is called structure template or design or pattern. This plays the same role as types ‘int’, ‘char’, ‘float’, etc.. We can, therefore, declare variable of this type, let’s see, how,

int main(void)
{
    struct NEW var1, var2; 
    /* 'var1', 'var2' are two variables of struct NEW */
}

We can also use keyword ‘typedef’ to give new type name of ours’ choice to the above structure declaration as,

/* structure declaration using typedef */
typedef struct NEW {
            int count;
            char ch;
            float avg;
            struct NEW *ptr;
    } Node;

‘Node’ is new type for the above structure declaration. Let’s declare variables of Node,

advertisement
int main(void)
{
    Node var1, var2;    /* 'Node' is new type */
}

Elements comprising the structure declaration are its members. Let’s initialize ‘var1’, a variable of ‘Node’, below,

int main(void)
{
    Node var2;
    Node var1 = {100, 'K', 67.58, &var2};
}

Let’s access structure ‘var1’ members, using dot ‘.’ operator. Dot ‘.’ is used to access members of a structure variable when it’s known. For example,

advertisement
    var1.count;    /* gives value 100 */
    var1.ch;       /* gives character 'K' */
    var1.avg;

Unlike arrays, name of a structure isn’t a pointer constant or address. To pass a structure as a function argument, either pass structure by value or pass pointer to structure. For example,

int main(void)
{
    Node var1, var2;
 
    /* passing pointer to structure */
    disp(&var1);
}
 
void disp(Node *p2s)    /* 'p2s' is a pointer-to-type-Node*/
{
    /* access members of 'var1' using "->" arrow operator */
    p2s->count;
    p2s->ch;
}
int main(void)
{
    Node var1, var2;
 
    /* passing structure by value */
    disp(var1);
}
 
void disp(Node temp)    /* 'temp' is a Node structure */
{
    /* accessing copies of members of 'var1' using dot operator */
    temp.count;
    temp.ch;

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.