473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Multi-Dimensional variable in struct in C program

I am stuck with how to access a 2-d array declared in a structure. For instance, I have declared a struct as follows:

Expand|Select|Wrap|Line Numbers
  1. struct sv {
  2. float a[10][10];
  3. };
Now in main function, how to access the variable 'a'? I tried the following

Expand|Select|Wrap|Line Numbers
  1. main () {
  2. struct sv * p;
  3. p = (struct sv *) malloc (sizeof(struct sv));
  4. *(p->*(a+1)+1) = 20.0;
  5. printf("%f\n",*(p->*(a+1)+1));
  6. }
But, this program gives the following error:
error: expected identifier before ‘*’ token
May 21 '10 #1
3 2499
Banfa
9,065 Expert Mod 8TB
Why don't you use

p->a[1][1] = 20.0;
May 21 '10 #2
Dear Banfa

Thanks for your reply. Your suggestion works well.

I also found an alternate way to do it. In fact somewhere in the web, I read that when declaring a struct, one has to know the array size apriori. But this is not true. For instance, the following program works
Expand|Select|Wrap|Line Numbers
  1. //---------------------------------------------------------------------------------------
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. struct sv {
  6. float **a;
  7. };
  8.  
  9. main () {
  10.    int i;
  11.    struct sv *p;
  12.    p = (struct sv *) malloc (sizeof(struct sv)*2);
  13.    p->a = (float **) malloc (sizeof(float)*10);
  14.    (p+1)->a = (float **) malloc (sizeof(float)*10);
  15.    for(i=0;i<10;i++)
  16.   {
  17.       *(p->a+i) = (float *) malloc (sizeof(float)*10);
  18.       *((p+1)->a+i) = (float *) malloc (sizeof(float)*10);
  19.       *(*((p+0)->a+i)+2) = i;
  20.       p->a[i][i]=i; //Suggested by Banfa 
  21.       printf("%f %f %f\n",*(*((p+1)->a+i)+2),*(*(p->a+i)+2),p->a[i][i]);
  22.    }
  23. }
  24. //-----------------------------------------------------------------------------------------
  25.  
The output will be

0.000000 0.000000 0.000000
0.000000 1.000000 1.000000
0.000000 2.000000 2.000000
0.000000 3.000000 3.000000
0.000000 4.000000 4.000000
0.000000 5.000000 5.000000
0.000000 6.000000 6.000000
0.000000 7.000000 7.000000
0.000000 8.000000 8.000000
0.000000 9.000000 9.000000

I spent one full day in this. I felt that I should share this experience with others so that they need not spent time at all. When I was stuck I found that there is no single book which talks about multi-dim array inside a structure. Also no online notes or manuals discuss about this. If anybody knows a good book or website which discusses multi-dim arrays inside a struct, kindly let me know.

Thanks in advance.

Baskar
May 22 '10 #3
Banfa
9,065 Expert Mod 8TB
While that is true you have not in fact declared any arrays in your last example only pointers which is why it would have been unlikely to come up during a search for arrays. When declaring an array you do need to know its size. There are some significant differences between arrays and pointers.

Arrays inside a structure work exactly the same as arrays outside a structure which is why you have not found the covered separately.

And finally you may want to try reading this which talks about multi-dimensional arrays a lot.
May 22 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: OutsiderJustice | last post by:
Hi All, I can not find any information if PHP support multi-thread (Posix thread) or not at all, can someone give out some information? Is it supported? If yes, where's the info? If no, is it...
37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
6
by: Joe | last post by:
I have 2 multi-list boxes, 1 displays course categories based on a table called CATEGORIES. This table has 2 fields CATEGORY_ID, CATEGORY_NAME The other multi-list box displays courses based on...
2
by: google | last post by:
Hello everyone, I am having an issue using the "Multi Select" option in a list box in MS Access 2003. I am making a form that users can fill out to add an issue to the database. Each issue can...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.