473,320 Members | 2,083 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,320 software developers and data experts.

returning an array of structures in c

10
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. struct pencil{
  4. int hardness;
  5. char maker;
  6. int number;
  7. };
  8.  
  9.  
  10.  
  11. struct pencil add(struct pencil pen);
  12.  
  13. int main(void){
  14. int i;
  15.  
  16. struct pencil pen;
  17.  struct pencil p[3];
  18.  
  19.  
  20. p=add(&pen);
  21.  
  22.  
  23.  
  24.  
  25. for (i = 0; i < 3; ++i){
  26.     printf("\n%d   %c%  d%", p[i].hardness, p[i].maker, p[i].number);
  27.    }
  28.  
  29. return 0;
  30.  
  31. }
  32.  
  33. struct pencil add(struct pencil pen){
  34. int i;
  35. int hard, make, num;
  36.  
  37. for(i =0; i< 3; i++){
  38.       printf("Enter pencil hardness :");
  39.     scanf("%d", hard);
  40.     printf("Enter pencil hardness :");
  41.     scanf("%c",make);
  42.     printf("Enter pencil hardness :");
  43.     scanf("%d",num);
  44.         strcpy(pen.hardness, pen.maker, pen.number,hard, make,num);
  45.     return pen;
  46. }
  47. }
  48.  
  49.  
I am trying to pass a structure to the function add() load values and return the structure to print in main. where I am calling the function this error is generated
C:\dm\bin>dmc c:\ma\r.c
p=add(&pen);
^
c:\ma\r.c(20) : Error: need explicit cast for function parameter 1 to get
from: struct pencil*
to : struct pencil
p=add(&pen);
^
c:\ma\r.c(20) : Warning 6: value of expression is not used
--- errorlevel 1
c:\ma\r.c(20) : Warning 6: value of expression is not used
--- errorlevel 1
Oct 29 '08 #1
2 7996
Ganon11
3,652 Expert 2GB
pen is a struct pencil.
&pen is the address of pen. It's type is struct pencil*.
add requires a struct pencil.

You are calling it by saying p=add(&pen), so you are passing it a struct pencil*.

This is not what you want to do.
Oct 29 '08 #2
donbock
2,426 Expert 2GB
By the way, some C compilers implement functions-returning-struct in a way that is not thread-safe. Your code will work fine in a single-threaded application, but fail intermittently when used in a multi-threaded application. I've gotten in the habit of never writing functions that return structures.

A compiler that has this problem will take the following code:
Expand|Select|Wrap|Line Numbers
  1. struct s {...};
  2.  
  3. struct s func(void) {
  4.    struct s var;
  5.    ...
  6.    return var;
  7.    }
  8.  
  9. ...
  10.    struct s foo;
  11.    foo = func();
  12. ...
And implement it as if you had written it like this:
Expand|Select|Wrap|Line Numbers
  1. struct s {...};
  2.  
  3. struct s *func(void) {
  4.    static struct s temp;
  5.    struct s var;
  6.    ...
  7.    temp = var;
  8.    return &temp;
  9.    }
  10.  
  11. ...
  12.    struct s foo;
  13.    foo = *func();
  14. ...
The thread-safety problem arises because of the static variable inside func. If one thread is preempted by another while it is executing func; and if that second thread also executes func; then the static variable will be corrupted from the point-of-view of the first thread.

It seems like I run into compilers with this problem a lot less now than I did 15 years ago. On the other hand, the problem might be as common as before but my personal coding standard of avoiding functions that return struct means that it doesn't affect me.

If your compiler has this problem then you need to worry about library functions, both those that came with the compiler and those from third parties. You typically don't have source code for libraries so you don't know if they're thread-safe.
Oct 29 '08 #3

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

Similar topics

1
by: Andrew Fleet | last post by:
Hi, I'm looking at returning a reference to an array I create within a subroutine. I could do this... sub foo { my @theArray; <snip>
4
by: Michael Trosen | last post by:
Hi Everyone, I hope someone can help, I'm pretty new to pro*c programming. I have the following application setup: a pro*c program calls a stored procedure and recieves a cursor back: the...
15
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
4
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
8
by: MyAlias | last post by:
Can't solve this CallBack returning structures Error message: An unhandled exception of type 'System.NullReferenceException' occurred in MyTest.exe Additional information: Object reference not...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
44
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
8
by: jodleren | last post by:
Hi! I have a function, a part of my code which I can use as a function. It will return 2 arrays, and I am wondering what way to do so. Both arrays hold strings, there are no special keys. 1)...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.