473,626 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multi-Dimensional variable in struct in C program

2 New Member
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 2508
Banfa
9,065 Recognized Expert Moderator Expert
Why don't you use

p->a[1][1] = 20.0;
May 21 '10 #2
apmsbask
2 New Member
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 Recognized Expert Moderator Expert
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
14566
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 possible to make doing multi-thread stuff? Thanks. YF
37
4867
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, Pujo
4
4653
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
3868
by: * ProteanThread * | last post by:
but depends upon the clique: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=954drf%24oca%241%40agate.berkeley.edu&rnum=2&prev=/groups%3Fq%3D%2522cross%2Bposting%2Bversus%2Bmulti%2Bposting%2522%26ie%3DUTF-8%26oe%3DUTF-8%26hl%3Den ...
0
3768
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 and white CCITT4 compressed files (frames) inside the tiff. Every now and then I receive a mixture of black and white CCITT4 and JPEG compressed files, and sometimes just multi-page tiffs with JPEG only. The code runs great when dealing with the...
6
8164
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
4877
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 a table called COURSES. This table has 2 fields CATEGORY_ID, COURSE_NAME. The CATEGORY_ID is a FK in COURSES and a PK in CATEGORIES. I want to populate the course list box based on any category(s)
2
3032
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 be associated with multiple categories. I have an "Issue," "IssueCategory," and "Category" in the database (among other tables). The form has a subform in it which is tied to the "IssueCategory" table. The main form is tied to the "Issue"...
5
5978
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 TIFF to several PNG files this causes a problem, becuase the resulting image is (the page to the far left and a lot of black space surrounding it and a filesize that is larger than needed. Any ideas?
1
9299
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 "PAR.UniqueID" could not be bound. The multi-part identifier "PAR.PAR_Status" could not be bound. The multi-part identifier "Salary.New_Salary" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part...
0
8192
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8696
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8502
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6119
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4195
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.