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

Using Structures in C

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
}
Aug 30 '07 #1
8 1764
gpraghuram
1,275 Expert 1GB
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
Aug 30 '07 #2
dmjpro
2,476 2GB
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.
Aug 30 '07 #3
JosAH
11,448 Expert 8TB
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
Aug 30 '07 #4
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.
Aug 31 '07 #5
RRick
463 Expert 256MB
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.
Aug 31 '07 #6
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.
Sep 1 '07 #7
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.
Sep 3 '07 #8
xoinki
110 100+
hi,
I think you need to typecast it back from (void *) to (myStructure *) in line number 10
regards,
Xoinki
Sep 3 '07 #9

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

Similar topics

9
by: timothy.williams | last post by:
Hi. I trying to write an extension module to call some C libraries so I can use them in Python. Several of the library functions pass pointers to structures as arguments. I was thinking that I...
11
by: Fred Bennett | last post by:
I have a simulation project in which data can naturally be held in structures for processing. There are calls to multiple functions involved. Execution speed is an issue. Do I take a big hit for...
1
by: kazack | last post by:
Hi all it's me again with another question as I got further in my book. The chapter I am in covers structres, abstract data and classes. I only read through to the end of the coverage on...
7
by: Ganesh Gella | last post by:
Hi All, I am planning to use Xalan to transform XML data by applying xls stylesheets. Here tricky part is, Xalan provides several C++ APIs, which are very much useful if our requirement is...
3
by: Matt D | last post by:
In my web service project I've imported an assembly (that I also have the source to) that contains structs that are serializable. I've created some web service methods that return and take as...
30
by: junky_fellow | last post by:
I was looking at the source code of linux or open BSD. What I found that lots of typedefs were used. For example, consider the offset in a file. It was declared as off_t offset; and off_t is...
11
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure...
6
by: DaTurk | last post by:
Hi, I'm coding a layer into my application using CLI, between a unmanaged and a c# layer. So, I have to marshal some unmanaged c++ structures to structures usable in c#. My solution was to...
2
by: =?Utf-8?B?dmxhZGltaXI=?= | last post by:
Hi, i have big subsystem written in old C and published by dll (and lib). Dll functions do: 1) allocate global memory for internal structures 2) controls dll subsystem (communicate by sockets,...
8
by: Bob Altman | last post by:
Hi all, I have a structure that includes a constructor. I want to add a bunch of these structures to an STL map (whose index is an int). If I define the map like this: map<int,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...

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.