473,395 Members | 1,418 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,395 software developers and data experts.

2D arrays of structs

1
Hey all,

I am having some issues trying to initialize and access a 2-dimensional array of structures (PageTableEntry's).

I have:
Expand|Select|Wrap|Line Numbers
  1. page_map = safe_malloc(num_processes * sizeof(PageTableEntry));
  2.     for(i=0;i<num_processes;i++)
  3.     {
  4.         page_map[i] = safe_malloc(num_virtual_pages * sizeof(PageTableEntry));
  5.     }
  6.  
  7. for(i=0;i<num_processes;i++)
  8.     {
  9.         for(j=0;j<num_virtual_pages;j++)
  10.         {
  11.             page_map[i][j] = NULL;
  12.         }
  13.     }
on compilation i get: error: incompatible types in assignment; at the line 11. I get a similar error when i attempt to assign:
page_map[i][j]->permissions = READ_WRITE; or any other assignment for that matter.

Any help would be greatly appreciated.
May 19 '07 #1
1 3149
AdrianH
1,251 Expert 1GB
Hey all,

I am having some issues trying to initialize and access a 2-dimensional array of structures (PageTableEntry's).

I have:
Expand|Select|Wrap|Line Numbers
  1.     page_map = safe_malloc(num_processes * sizeof(PageTableEntry));
  2.     for(i=0;i<num_processes;i++)
  3.     {
  4.         page_map[i] = safe_malloc(num_virtual_pages * sizeof(PageTableEntry));
  5.     }
  6.  
  7.     for(i=0;i<num_processes;i++)
  8.     {
  9.         for(j=0;j<num_virtual_pages;j++)
  10.         {
  11.             page_map[i][j] = NULL;
  12.         }
  13.     }
on compilation i get: error: incompatible types in assignment; at the line 11. I get a similar error when i attempt to assign:
page_map[i][j]->permissions = READ_WRITE; or any other assignment for that matter.

Any help would be greatly appreciated.
Not knowing the type that you declared page_map as, I am assuming that you declared it as a 2d array of structs? If so, if you dereference the array twice with the [] operator, it becomes a user defined struct type, not a pointer to a user defined struct type.

I.e.
Expand|Select|Wrap|Line Numbers
  1. struct A {
  2.   int x;
  3. };
  4.  
  5. A a[1][4];
  6. a[0][2] = NULL; // invalid since NULL cannot be assigned to a struct A type.
  7.  
  8. // Similarly...
  9. A** a2;
  10. a2 = (struct A**)malloc(sizeof(A**)*1);
  11. a2[0] = (struct A*)malloc(sizeof(A*)*4);
  12. a2[0][2] = NULL; // invalid since NULL cannot be assigned to a struct A type.
  13.  

Adrian
May 19 '07 #2

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

Similar topics

1
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
3
by: Zenon | last post by:
I have a function which returns array of structs. I need to create a collection of those arrays and thought that an ArrayList would be a good way to do this since the count is variable. The...
3
by: Nyx18 | last post by:
what im trying to do is read in a data from a file into 4 different arrays then also read in the same data using array of structs. the assignment is to show that we know how to use both methods of...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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...

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.