1

I have a Structure say MyStruct:-

#define SEC_DIMENSION 2
struct MyStruct
{
  char cChar;
  float fFloat;
  int   iInt;
};

struct MyStruct StructArray[SEC_DIMENSION][20];  //Two Dimensional Array of Structures.

Now I want to access this with Pointer.

struct MyStruct *StructPtr[SEC_DIMENSION];

I did assignment as follows:-

 StructPtr[0] = &StructArray[0][0];

Now, I want to access Members of Structure StructArray[0][1] i.e. StructArray[0][1].cChar or StructArray[0][1].fFloat

How can I access them by using StructPtr?

I tried using StuctPtr[0][1]->cChar then ((StructPtr[0])[1])->cChar Both returned an error.

With StructPtr[0]->cChar build was successful. But this is not what I want.

4
  • "But this is not what I want." , what do you want exactly ? Commented Sep 7, 2011 at 5:46
  • I want to Access StructArray[0][1].cChar using Pointers. In This case It would be accessing StructArray[0][0].cChar not for Other Members of This Array. Commented Sep 7, 2011 at 5:49
  • you have yourself assigned the address of the structure at array location [0][0] into the 0th element of the array of pointers StructPtr. Therefore StructPtr[0] now holds the structure StructArray[0][0] 's base address. Therefore StructPtr[0]->cChar will give you the cChar component of StructArray[0][0] Commented Sep 7, 2011 at 5:52
  • @Swanand Purankar: Huh? Why is it StructArray[0][1] in the first case, and then suddenly StructArray[0][0]? Sorry, this is all still overly confusing. Provide a better descripotion of what you are trying to do. Commented Sep 7, 2011 at 5:52

2 Answers 2

1

"Now I want to access this with Pointer" is not very descriptive. What you have in your example will not work. What you declared is not a pointer to an array but rather an array of pointers. Is that what you need? I don't know.

If you really need a pointer to an array, you can declare your pointer it as

struct MyStruct (*StructPtr)[20];

and then make it point to your array as

StructPtr = StructArray;

From this point on you can access the original array through this pointer as StructPtr[i][j]

StructPtr[i][j].cChar = 'a';

Alternatively, you can declare the pointer as

struct MyStruct (*StructPtr)[SEC_DIMENSION][20];

and then make it point to your array as

StructPtr = &StructArray;

From this point on you can access the original array through this pointer as (*StructPtr)[i][j]

(*StructPtr)[i][j].cChar = 'a';
Sign up to request clarification or add additional context in comments.

4 Comments

But struct MyStruct (*StructPtr)[SEC_DIMENSION][20] will declare (SEC_DIMENSION*20) pointers, Right? Instead of That 20, I want a Single Pointer with Which I could access All 20 structures and their members!
@Swanand Purankar: No. You need to get more familiar with C declarations first. struct MyStruct (*StructPtr)[SEC_DIMENSION][20] declares exactly one pointer. It will declare exactly one pointer that points to type struct MyStruct[SEC_DIMENSION][20].
If you do struct MyStruct *StructPtr[SEC_DIMENSION][20], then you'll get SEC_DIMENSION*20 pointers. But one you add those extra parentheses, the meaning of the declaration changes drastically. How you get just one pointer.
Okey! Thanks! And Thanks for Clearing a Concept!!
1

I think you need a "pointer to an array of dimension [SEC_DIMENSION][20] of structures of type struct MyStruct":

struct MyStruct (*StructPtr)[SEC_DIMENSION][20];

StructPtr = StructArray;

StructPtr[i][j]->cChar;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.