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: - typedef struct {
-
char name[32];
-
int employeeID;
-
} employee;
My main program creates an array of the employees, with the line: - employee employeeTable[MAX_EMPLOYEES];
I am trying to call my displayTable function like so: - displayTable(employeeTable);
The displayTable function is given below: - void displayTable(const employee *empTable){
-
int i;
-
int key;
-
for(i=0;i<MAX_EMPLOYEES;i++){
-
key = empTable[i].employeeID;
-
if(key == -1){
-
printf("Location #: %d\tEmpty\n",i);
-
}else if(key == -2){
-
printf("Location #: %d\tPreviously Deleted\n",i);
-
}else{
-
printf("Location #: %d\t%d\t%s\n",i,key,empTable[i].name);
-
}
-
}
-
}
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?
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.
no such luck...any other suggestions?
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.
gcc...i'm using bloodshed for the coding
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.
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.
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.
RRick 463
Recognized Expert Contributor
In C, if you call a function before you declare it, the compiler assumes it has the form
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.
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!
Sign in to post your reply or Sign up for a free account.
Similar topics |
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?
|
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
|
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;
|
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()
|
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;
};
| |
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...
|
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;
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |