Connecting Tech Pros Worldwide Forums | Help | Site Map

Using Structures in C

Newbie
 
Join Date: Aug 2007
Posts: 1
#1: Aug 30 '07
Hi

does the below structure variable access declaration correct..does it retrieve the value of var if I kept struct var in bracket..let me know please



if(*(str_src->var) !='')
{
//do some thing
}

gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#2: Aug 30 '07

re: Using Structures in C


Quote:

Originally Posted by dvnsatish

Hi

does the below structure variable access declaration correct..does it retrieve the value of var if I kept struct var in bracket..let me know please



if(*(str_src->var) !='')
{
//do some thing
}

If u derefernce the pointer then u shuld use . operator.
So the declaration shuld be
(*str_src).var

Raghuram
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#3: Aug 30 '07

re: Using Structures in C


Quote:

Originally Posted by dvnsatish

Hi

does the below structure variable access declaration correct..does it retrieve the value of var if I kept struct var in bracket..let me know please



if(*(str_src->var) !='')
{
//do some thing
}

Welcome to TSDN.

You would be better to post the whole Code.
Anyway, have a look at the code given below.

Expand|Select|Wrap|Line Numbers
  1. struct A
  2. {
  3. int a,b,c;
  4. };          //Structure Declaration.
  5. A a;
  6. //now to access the member variable use a.a or a.b or a.c.
  7. A *a=(A*)malloc(sizeof(A));
  8. //now to access the member variable a->a or a->b or a->c.
  9.  
Now I think you got me.

Kind regards,
Dmjpro.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Aug 30 '07

re: Using Structures in C


Quote:

Originally Posted by gpraghuram

If u derefernce the pointer then u shuld use . operator.
So the declaration shuld be (*str_src).var

You can't ignore the parentheses; *(a->b) isn't the same as (*a).b

kind regards,

Jos
Newbie
 
Join Date: Sep 2006
Location: Doing a software contract in Logan, UT.
Posts: 10
#5: Aug 31 '07

re: Using Structures in C


Quote:

Originally Posted by dmjpro

Welcome to TSDN.

You would be better to post the whole Code.
Anyway, have a look at the code given below.

Expand|Select|Wrap|Line Numbers
  1. struct A
  2. {
  3. int a,b,c;
  4. };          //Structure Declaration.
  5. A a;
  6. //now to access the member variable use a.a or a.b or a.c.
  7. A *a=(A*)malloc(sizeof(A));
  8. //now to access the member variable a->a or a->b or a->c.
  9.  
Kind regards,
Dmjpro.

Greetings:
I'm trying to get a handle on passing an array of pointers to structures between functions.

The following code compiles w/out errors and gives a result.
Notice that I didn't use dynamic memory storage (heap), but using the Stack.

I believe that is the correct assumption.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. void *myFunction(unsigned i);
  4. typedef struct {
  5.     char* name;   /* '\0'-terminated C string */
  6.     int   number;
  7. } SomeSeq;
  8.  
  9. int main (int argc, const char * argv[]) {
  10.     // insert code here...
  11.     printf("Hello, World!\n");
  12.     printf("1) myFunction says: %s\n",myFunction(1));
  13.     printf("2) myFunction says: %i\n",myFunction(2));
  14.     SomeSeq *myStructure = myFunction(3);
  15.     printf("3) myFunction says: %s and %i.\n",myStructure->name,myStructure->number);
  16.     printf("Done.\n");
  17.     return 0;
  18. }
  19.  
  20. void *myFunction(unsigned i) {
  21.     printf("\n{myFunction}");        
  22.     if (i == 1) {
  23.         // Character String.
  24.         return "hello\0";        
  25.     } else if (i == 2) {
  26.         // Integer.
  27.         return (int*)555; // Note the int pointer cast of a void pointer.
  28.     } else {
  29.         // Structure.
  30.         SomeSeq *myStruct; 
  31.         myStruct->name = "Frederick C. Lee";
  32.         myStruct->number = 123;
  33.         return (SomeSeq *)myStruct;
  34.     }
  35. }
  36.  
The output is:

Expand|Select|Wrap|Line Numbers
  1. run
  2. [Switching to process 662 local thread 0xf03]
  3. Running…
  4. Hello, World!
  5.  
  6. {myFunction}1) myFunction says: hello
  7.  
  8. {myFunction}2) myFunction says: 555
  9.  
  10. {myFunction}3) myFunction says: Frederick C. Lee and 123.
  11. Done.
  12.  
  13. Debugger stopped.
  14. Program exited with status value:0.
  15.  
...However, I need to use dynamic memory (heap):

Expand|Select|Wrap|Line Numbers
  1.       ....
  2.     } else {
  3.         // Structure.
  4.         // SomeSeq *myStruct; 
  5.         SomeSeq *myStruct=(SomeSeq*)malloc(sizeof(SomeSeq));   // Using memory.
  6.         myStruct->name = "Frederick C. Lee";
  7.         myStruct->number = 123;
  8.         return (SomeSeq *)myStruct;
  9.     }
  10.  
I get the same output.
But this time I'm getting the compiler warning:
Expand|Select|Wrap|Line Numbers
  1. "warning: incompatible implicit declaration of built-in function 'malloc'"
Why? I looks like I'm type-casting okay.

Also, I 'free' the memory in main() after the function call:

Expand|Select|Wrap|Line Numbers
  1. int main (int argc, const char * argv[]) {
  2.     // insert code here...
  3.     printf("Hello, World!\n");
  4.     printf("1) myFunction says: %s\n",myFunction(1));
  5.     printf("2) myFunction says: %i\n",myFunction(2));
  6.     SomeSeq *myStructure = myFunction(3);
  7.     printf("3) myFunction says: %s and %i.\n",myStructure->name,myStructure->number);
  8.     free(myStructure);  // Freeing Stucture memory <---
  9.     printf("Done.\n");
  10.     return 0;
  11. }
  12.  
Is this good code? Why am I getting the compiler warning?

Ric.
RRick's Avatar
Expert
 
Join Date: Feb 2007
Posts: 430
#6: Sep 1 '07

re: Using Structures in C


UncleRic, I'm assuming you are compiling this with C++ because you are using the "//" comments.

For C++, the problem is that malloc is not found in stdio.h but int stdlib.h. Add that library and the error should go away.

Why aren't you using new and delete? Those are the standard C++ heap managing routines.
Newbie
 
Join Date: Sep 2006
Location: Doing a software contract in Logan, UT.
Posts: 10
#7: Sep 1 '07

re: Using Structures in C


Quote:

Originally Posted by RRick

UncleRic, I'm assuming you are compiling this with C++ because you are using the "//" comments.

For C++, the problem is that malloc is not found in stdio.h but int stdlib.h. Add that library and the error should go away.

Why aren't you using new and delete? Those are the standard C++ heap managing routines.

I'm using Apple's Xcode 2.4.1 (gcc 4.1?) to compile *purely* ANSI C code. I come from an ObjC background. I didn't know 'C' doesn't use //. It compiled okay.

Perhaps that's given me the unneeded errors: code I write has warnings vs pasted code from Internet tend to be okay.

Anyway, I'm trying to work with structures in a pure 'C' environment (and avoid the pidgin).

Hence I'm working with:
#include <stdio.h>
...and optionally:
#include <string.h>


Regards,

Ric.
Newbie
 
Join Date: Sep 2006
Location: Doing a software contract in Logan, UT.
Posts: 10
#8: Sep 3 '07

re: Using Structures in C


I'm trying to figure out how to create a usable array of pointers to structures.

Essentially, I want to call a function that return a usable array of pointers to structures.

From what I've learned so far, you allocate the structure array by doing a malloc of the structure, multiplied by the number of elements of the array as indicated in the following code:

Expand|Select|Wrap|Line Numbers
  1. typedef struct {
  2.         char* name;   /* '\0'-terminated C string */
  3.         int   number;
  4.     } myStructure;
  5.  
  6. void *myFunction(int n);
  7.  
  8. int main() {
  9.    int n = 5;
  10.    myStructure *myData[n] = myFunction(n);    // ...correct?
  11.    // ....
  12.    free(myData);
  13. } // main()
  14.  
  15. // ------------------------------------------------------------------------------------
  16.  
  17. void *myFunction(int n) {
  18.  
  19.     // I want to reserves space for n 'myStructures' via the following syntax.
  20.     // Shouldn't use [] in declaration; hence the 'n' counter in the sizeof().
  21.  
  22.     myStructure *ricData = (myStructure *)malloc(sizeof(myStructure)*n);
  23.  
  24.     // This works:
  25.     ricData->number = 123;
  26.     ricData->name = "Ric Lee\0";
  27.  
  28.     // This  also works:
  29.     printf("\nricData[0].name= %s",ricData[0].name);
  30.  
  31.     // Load value into [1]:
  32.     ricData[1].number = 345;
  33.     ricData[1].name = "Richard D. Brauer\0";
  34.  
  35.     // This also works:
  36.     printf("\nricData[1].name= %s\n",ricData[1].name);
  37.  
  38.     // What happens when I load BEYOND 5 elements?
  39.     ricData[7].number = 777;
  40.     ricData[7].name = "Mydle Oogalbee";
  41.  
  42.     printf("\nricData[7].name= %s",ricData[7].name);  // Debugger sees it. 
  43.  
  44.     return &ricData;     // Is this correct: addr to array of pointers to structures?
  45.  
  46. } // end myFunction().
  47.  
  48.  
I'm not sure if this is the correct syntax. I purposely loaded values beyond the array size to see what would happen. The debugger showed the mapping to be successful. But enabling the 'Malloc Guard' option to the debugger flagged this error.

1) Am I correctly creating & loading structures of an array of ptrs, into dynamic memory?

2) Do I return the address (&struct) of the array of structure pointers to the calling program?

Regards,

Ric.
Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 113
#9: Sep 3 '07

re: Using Structures in C


hi,
I think you need to typecast it back from (void *) to (myStructure *) in line number 10
regards,
Xoinki
Reply