Array Declaration and pointer assignment in C

10,556

Solution 1

You should know operator precedence rules in C: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedencehttp://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence

int (*ptr)[3] as opposed to int * ptr [3]

The first one is a pointer (notice * is closer to the var name) to arrays of int of size 3 The second one is equal to int (*(ptr [3])) which is an array of size 3 on int pointers.

You can also use this site: http://cdecl.org/ if you have doubts on how to interpret an expression.

Solution 2

1. Bidimensional array:

int a[2][3]= { {1,2,3},{4,5,6}};

With this statement in memory you have 2x3 integers, all adjacent in memory.I suppose that you know how to access them, but in the case you don't I'll clarify it:

a[0][0] : 1
a[0][1] : 2
a[0][2] : 3
a[1][0] : 4
a[1][1] : 5
a[1][2] : 6

2. Pointer to array:

int (*ptr)[3] = &a[0];

ptr points to a int[3] block of memory.So you can assign it only to an int[3] type:

ptr= &a[0];
ptr= &a[1];

The difference is that this pointer does not have it's own memory, and you have to assign it to an int[3] variable or allocate it:

ptr= malloc (2*sizeof(int[3]);

This way you can use the memory pointed by ptr, if you initialize ptr this way:

for(int j=0; j<2; j++)
    for(int i=0; i<3;i++)
        ptr[j][i]=i+j*3+1;

This case you'll have the same memory representation of int a[2][3], except that this memory is in the heap and not in the stack.You can always choose to realloc/free the memory and this memory is not deleted once your function terminates.

Share:
10,556
Mohit Sehgal
Author by

Mohit Sehgal

I am End to End Software developer with expertise in MongoDB, ExpressJS, NodeJS, VueJS, ElectronJS, C, C++, Java, Python, and more. I can develop quality software, web apps, and mobile apps. I can help you in the backend, web frontend as well as Android Apps. I can get your project the latest, attractive and sleek design. I have experience of working with AWS, Amazon S3, Amazon EC2, Digital Ocean, NGINX, PM2, UFW, Amazon IAM, SES, and other DevOps services. I have published many Android Apps on Google Play Store. I am interested in solving general problems with the help of my skills. I believe in possibilities. Currently, I am working on Android Application Development. My core competency lies in complete end-end management of a new mobile application development project, and I am seeking opportunities to build applications from the ground up for you or your business. I have done Android App Development in the following categories: Productivity Apps. Apps for Enterprises Social Apps Chat Apps. Custom Keyboards Photo Editing App GPS based Apps I have worked on Java, Android Platform, Google App Engine, Java Struts Framework, JavaScript, Python, Groovy/Grails and bit of iOS Programming. I like to learn a variety of programming languages. I'll like to program IoT, Machine Learning, Artificial Intelligence, and Game Development. Steve Jobs is my idol because he had a strong belief in himself and his intuitions. I want to become a pioneer in technology like him. I blog on https://appsyoda.com and https://developers.appsyoda.com

Updated on June 04, 2022

Comments

  • Mohit Sehgal
    Mohit Sehgal almost 2 years

    I am confused in the basics of pointer and array declaration in C. I want to know the difference between following two statements except that base address to array is assigned to ptr in seconed statement.

    int a[2][3]= { (1,2,3),(4,5,6)};
    int (*ptr)[3] = &a[0];
    

    Please quote examples to clarify. What effect do [3] on R side of line 2 has?