473,769 Members | 3,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem passing an array of structs

5 New Member
I am trying to write a C program that makes use of an array of structs, but I am having a problem passing the array as a parameter. The struct is an employee, defined in a header file as:

Expand|Select|Wrap|Line Numbers
  1.    typedef struct {
  2.            char name[32];
  3.            int  employeeID;
  4.    } employee;
My main program creates an array of the employees, with the line:

Expand|Select|Wrap|Line Numbers
  1.     employee employeeTable[MAX_EMPLOYEES];
I am trying to call my displayTable function like so:

Expand|Select|Wrap|Line Numbers
  1.    displayTable(employeeTable);
The displayTable function is given below:

Expand|Select|Wrap|Line Numbers
  1.    void displayTable(const employee *empTable){
  2.         int i;
  3.         int key;
  4.         for(i=0;i<MAX_EMPLOYEES;i++){
  5.            key = empTable[i].employeeID;
  6.            if(key == -1){
  7.               printf("Location #: %d\tEmpty\n",i);
  8.            }else if(key == -2){
  9.               printf("Location #: %d\tPreviously Deleted\n",i);
  10.            }else{
  11.               printf("Location #: %d\t%d\t%s\n",i,key,empTable[i].name);
  12.            }
  13.         }
  14.    }
The compiler is claiming incompatible types. I searched the net, trying to find an example of how to do this correctly. All of the examples I saw omitted the typedef command, and then added the word struct to the parameter of the function. I believe that using the typedef statement allows me to omit this, but I am not able to compile. Any suggestions?
Oct 9 '07 #1
10 2469
Ganon11
3,652 Recognized Expert Specialist
Instead of using const employee* empTable in the function header, try const employee empTable[]? If that doesn't work, I dunno.
Oct 10 '07 #2
joeSquid
5 New Member
no such luck...any other suggestions?
Oct 10 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
no such luck...any other suggestions?
What compiler are you using??

Your code in your original post compiles and links without error using Visual Studio.NET 2005.
Oct 10 '07 #4
joeSquid
5 New Member
gcc...i'm using bloodshed for the coding
Oct 10 '07 #5
Banfa
9,065 Recognized Expert Moderator Expert
The code looks good to me too, you may want to post the exact errors you are getting, it may help us.
Oct 10 '07 #6
joeSquid
5 New Member
All right; I solved it. The call to the displayTable function appeared before the actual declaration of the function. Apparently the compiler was making some assumption about the code, and was preventing it from compiling. I added the line:

void displayTable(co nst employee*);

before (and outside of) main, and now it compiles without problem. Anybody know exactly what was going on? And thanks for letting me know that it compiled okay; I wouldn't have thought to try that otherwise.
Oct 10 '07 #7
drhowarddrfine
7,435 Recognized Expert Expert
You got that error because the function wasn't defined before use. gcc will report it, in certain instances, as incompatible type when it's really looking for a definition, which you eventually provided.
Oct 10 '07 #8
RRick
463 Recognized Expert Contributor
In C, if you call a function before you declare it, the compiler assumes it has the form
Expand|Select|Wrap|Line Numbers
  1. int calledFunc();
When you define the function later, the compiler compares that new declaration to the one it created before. In your case, the compiler probably didn't like the fact that your function didn't return an int that it was expecting.
Oct 11 '07 #9
joeSquid
5 New Member
Thanks for the help...coming from c++, I normally make a habit of placing my function definitions in a header file before I write my program. When I started learning c, I began getting in the habit of declaring my functions right before main (not sure why I started doing it this way). I forgot to do even that with this program; my first few functions all returned ints; thus gave me no similar errors. I see why now. Thanks for the insight!
Oct 12 '07 #10

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

Similar topics

19
1760
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my types be arbitraily big as well?
2
1605
by: spleen | last post by:
Hiya, Im a newbie to C so please excuse my ignorance... Ok, so I have several C files, with my functions in, one has main in of course! and I have 2 structs as outside of the funtions (therefore global?) in one of the files. I can pass my information into the structs with no major difficulties, that was easy! anyway now im trying to read from the structure into a
5
3130
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
2
6067
by: WTH | last post by:
I have a C# webservice that returns an array of struct data back to a calling client. It works fine when tested via the ASMX page; however, I don't know how to make the call to this particular method from C++. If my web method is declared as: public MyStruct GetData()
17
2812
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to free()? #include <stdlib.h> struct stype { int *foo; };
3
4425
by: cskarp | last post by:
I have create a .Net component which exposes an interface with two methods. The first method takes an array of structs as a parameter the other method returns an array of (the same tyoes of) structs. When I get the array structures from my second method in unmanaged c++, I cannot regonize any of the parameters in the structures. But if I pass the same pointer to the first method in my interface and look at the values in .net then thay are...
8
3503
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
4359
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 structs. The array of structs are allocated by C#. If I pass the array without 'ref' it works fine on the C++ side, but after returning to C# the array struct values are all zero. If I pass with 'ref' the application crashes. If I use a class...
0
1660
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved it for me (using the ref keyword etc). I now encountered a function that takes a pointer to an array as a parameter, and this array consists of other pointers, to structs. Horrible isn't it? I have no way of modifying the unmanaged code, I...
0
9422
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10035
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.