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

Passing a dynamic array of struct to a function, the returning the same array

In the 'calling()' function I declare the following pointer:
Expand|Select|Wrap|Line Numbers
  1. struct tags {
  2. char aname[100];
  3. int aval;
  4. .
  5. .
  6. .
  7. }*tgs;
  8.  
I then call the 'called()' function like such:
Expand|Select|Wrap|Line Numbers
  1. tgs = struct tags get_id_tags(buf, tgs);
  2.  
The 'called()' function looks like this:
Expand|Select|Wrap|Line Numbers
  1. struct tags *get_id_tags(char *bu, struct tags *tg) {
  2. /* statements here*/
  3. return tg;
  4. }
  5.  
When I try to compile, I get the following warnings:
"warning: passing argument 2 of 'get_id_tags' from incompatible pointer type"
"warning: assignment from incompatible pointer type"
both regard the function call.
I have tried with various level of indirection, but unsuccessfully.
I have used such structures before, but either as globals, or processed in the same function.
I simply cannot understand what I,m doing wrong, and would be grateful if
some good soul can help me out.
I'm not a student (unfortunately), i'm 65.
Thank anybody for the attention.
Regards Sergio (trycstruct).
Mar 10 '10 #1
6 2381
Banfa
9,065 Expert Mod 8TB
For starters this
Expand|Select|Wrap|Line Numbers
  1. tgs = struct tags get_id_tags(buf, tgs);
should be this
Expand|Select|Wrap|Line Numbers
  1. tgs = get_id_tags(buf, tgs);
That is the only error I see in what is posted, I assume you pre-declare get_id_tags before trying to call it? A predeclaration looks like this
Expand|Select|Wrap|Line Numbers
  1. struct tags *get_id_tags(char *bu, struct tags *tg);
It declares the functions return type and paramater types to allow the compiler to do type checking.

You don't have to be a student to post here and anyway surely you can still be a student at 65 if you are still learning anything ;)
Mar 10 '10 #2
Thank for replying banfa.
I had made a mistake while copying & pasting.
The struct in the program is the following:
Expand|Select|Wrap|Line Numbers
  1.   struct tags {
  2.     char tg[TG_NAM];
  3.     char id[ID_LEN];
  4.     char class[ID_LEN];
  5.     char col[COL_NAM];
  6.     int op;
  7.     int cl;
  8.     int siz;
  9.   }*alltgs;
  10.  
The predeclaration looks like this:
Expand|Select|Wrap|Line Numbers
  1. struct tags *get_id_tags(char *tbuf, struct tags *stt);
  2.  
The actual call looks like this:
Expand|Select|Wrap|Line Numbers
  1. alltgs = get_id_tags(hbuf, alltgs);
  2.  
And the (not yet written) function looks like this:
Expand|Select|Wrap|Line Numbers
  1. struct tags *get_id_tags(char *tbuf, struct tags *stt) {
  2.  
  3.   return stt;
  4. }
But the compiler complains about, and I have tried all sort of combination of '*' and '&'.
Mar 10 '10 #3
Banfa
9,065 Expert Mod 8TB
OK well I took that code and produced

Expand|Select|Wrap|Line Numbers
  1. #define TG_NAM 20
  2. #define COL_NAM 20
  3. #define ID_LEN 20
  4.  
  5. struct tags {
  6.     char tg[TG_NAM];
  7.     char id[ID_LEN];
  8.     char class[ID_LEN];
  9.     char col[COL_NAM];
  10.     int op;
  11.     int cl;
  12.     int siz;
  13. }*alltgs;
  14.  
  15. struct tags *get_id_tags(char *tbuf, struct tags *stt);
  16.  
  17. int main()
  18. {
  19.     char hbuf[50];
  20.     alltgs = get_id_tags(hbuf, alltgs);
  21.     return 0;
  22. }
  23.  
  24. struct tags *get_id_tags(char *tbuf, struct tags *stt) {
  25.  
  26.   return stt;
  27. }
  28.  
In which I just add a few #defines for array sizes and put a main round your function call and this compiles without errors.

You haven't accidentally declared a variable name tgs in the code calling your function, this would hide the global declaration of tgs.

Also use of the word class as a member name in your structure will prove troublesome if you ever try to convert this project to C++.
Mar 10 '10 #4
Banfa
9,065 Expert Mod 8TB
You should have posted this not put it in a PM to me
I have written a small, (do nothing) program that yields exactly the same compiler warning messages that my real program yields.
So the question this time would be:
How can I get this (do nothing) program to compile without warnings, and is it possible at all?
If I declare the 'struct tags' in the program's global area, the program compiles cleanly; I know, I have done that a number of times before, I just wanted to know why I cannot do it this way.
So if you got to this point, following is the program:
Expand|Select|Wrap|Line Numbers
  1. /* test-stru.c */
  2.  
  3. #include <stdlib.h>             /* For 'NULL' (below) */
  4.  
  5.  
  6. void func1(char *fil);
  7. struct tags *get_id_tags(char *tbuf, struct tags *stt);
  8.  
  9. /* ************************************************************************ */
  10. int main(int argc, char **argv) {
  11.   char buff[256] = {0};
  12.  
  13.   /* Lots of code here; i.e. to copy argv[??] int 'buff', etc.. */
  14.   func1(buff);
  15.  
  16.   return 0;
  17. }
  18.  
  19. /* ************************************************************************ */
  20. void func1(char *fil) {
  21.   char *hbuf = NULL;
  22.   struct tags {
  23.      char tg[32];
  24.      int siz;
  25.  }*alltgs;
  26.  
  27.   /* malloc 'hbuf' to the size of 'fil', then read in 'fil' */
  28.   /* Call 'get_id_tags() to fill in 'alltgs' */
  29.  
  30.   alltgs = get_id_tags(hbuf, alltgs);
  31.  
  32.   /* Free everything  */
  33. }
  34.  
  35. /* ************************************************************************ */
  36.  struct tags *get_id_tags(char *tbuf, struct tags *stt) {
  37.  
  38.    /* Scan 'tbuf' and fill in 'stt' */
  39.  
  40.    return stt;
  41.  }
  42.  
And again, following are the warning messages:
Expand|Select|Wrap|Line Numbers
  1. test-stru.c: In function 'func1':
  2. test-stru.c:30: warning: passing argument 2 of 'get_id_tags' from incompatible pointer type
  3. test-stru.c:30: warning: assignment from incompatible pointer type
  4.  
The compiler I use is the GNU 'gcc'.
Mar 11 '10 #5
Banfa
9,065 Expert Mod 8TB
However creating a small do nothing program that produced the same behaviour is probably the best thing you could have done as far as demonstrating the problem and it is in fact now clear what the problem is.

At line 7 of your code listing you try to use the type struct tags. However it is not declared until line 22.

Line 22 - 25 defines a structure and declares a variable. You need to split this up because you need to define the structure at the head of the file before any references to it, around about line 4 - 5. However you need to keep the variable declaration at line 22.

Split this at line 22

Expand|Select|Wrap|Line Numbers
  1. struct tags {
  2.     char tg[32];
  3.     int siz;
  4. }*alltgs;
  5.  
Into this at line 5
Expand|Select|Wrap|Line Numbers
  1. struct tags {
  2.     char tg[32];
  3.     int siz;
  4. };
  5.  
and this at line 22
Expand|Select|Wrap|Line Numbers
  1. struct tags* alltgs;
  2.  
Mar 11 '10 #6
Thank you for your help, it was very appreciated.
trycstruct
Mar 12 '10 #7

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

Similar topics

4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
2
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
2
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of...
5
by: ctj951 | last post by:
I have a very specific question about a language issue that I was hoping to get an answer to. If you allocate a structure that contains an array as a local variable inside a function and return...
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.