473,748 Members | 9,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning structs containing arrays

It's the first time I try using structs, and I'm getting confused with
it and can't make it work properly

I firstly define the structure by this : typedef struct { char *l1; int
*l2; int Nval; } *arrays;

It's supposed to be a structure containing an array of chars, an array
of ints and an int.

I declare functions like this : arrays *parseline(char *line, int N)

and I point to the arrays I want to return like this :

out.l1=displayl n;
out.l2=sumline;
out.Nval=Nval;
return out;

When compiling it gives me an error, such as "error: request of member
<< l1 >> in something that's not a structure or an union". What's wrong
with the way I do it and how should I fix it?

Nov 20 '05 #1
3 2845
Michel Rouzic wrote:

It's the first time I try using structs,
and I'm getting confused with
it and can't make it work properly

I firstly define the structure by this :
typedef struct { char *l1; int *l2; int Nval; } *arrays;

It's supposed to be a structure containing an array of chars,
an array of ints and an int. I declare functions like this : arrays *parseline(char *line, int N) out.l1=displayl n;
out.l2=sumline;
out.Nval=Nval;
return out; and how should I fix it?


struct arrays {
char c_array[l1];
int i_array[l2];
int Nval;
};

#include <string.h>

struct arrays out;

memcpy(out.c_ar ray, displayln, sizeof out.c_array);
memcpy(out.i_ar ray, sumline, sizeof out.i_array);
out.Nval = Nval;
return out;
.... or sizeof the source object, whichever is smaller.
If displayln is the name of a smaller array then

memcpy(out.c_ar ray, displayln, sizeof displayln);

If displayln is the name of a pointer
to the first element of a smaller array
then you have figure something out.
Likewise for out.i_array.

--
pete
Nov 20 '05 #2
Michel Rouzic wrote:
It's the first time I try using structs, and I'm getting confused with
it and can't make it work properly

I firstly define the structure by this : typedef struct { char *l1; int
*l2; int Nval; } *arrays; ^^^
The * says pointer, why put it there?

Also, l1 and l2 are pore choices for names, since they look too much
like 11 and 12.
It's supposed to be a structure containing an array of chars, an array
of ints and an int.
Well, what you have specified is pointer to char and pointer to int. I
suggest you reread what your text books say about arrays, pointers and
declarations of them.
I declare functions like this : arrays *parseline(char *line, int N)

and I point to the arrays I want to return like this :

out.l1=displayl n;
out.l2=sumline;
out.Nval=Nval;
return out;

When compiling it gives me an error, such as "error: request of member
<< l1 >> in something that's not a structure or an union". What's wrong
with the way I do it and how should I fix it?


Quick answer, remove the extraneous *. Although I'll bet that leaves you
with lots of other problems. See my comments above.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 20 '05 #3
"Michel Rouzic" <Mi********@yah oo.fr> writes:
It's the first time I try using structs, and I'm getting confused with
it and can't make it work properly

I firstly define the structure by this : typedef struct { char *l1; int
*l2; int Nval; } *arrays;
Which is much more legible like this:

typedef struct {
char *l1;
int *l2;
int Nval;
} *arrays;
It's supposed to be a structure containing an array of chars, an array
of ints and an int.
Your structure type doesn't *contain* an array of anything. It
contains two pointers and an integer. The pointers might point to
arrays if you initialize them to point to allocated memory.

The typedef "arrays" doesn't name a structure type; it names a
pointer-to-structure type. You haven't provided a name for the
structure type.

You should consider dropping the typedef altogether and just using
the structure tag directly, something like:

struct arrays {
char *l1;
int *l2;
int Nval;
};

You would then refer to the type as "struct arrays" rather than
"arrays". The typedef saves you the effort of typing "struct", but
that's really not much of a benefit; your code is clearer if it's
obvious that the type is a structure. (There are rare cases where you
might want to hide the nature of the type; this isn't likely to be one
of them.)

You should also choose better names. Flash Gordon has already
mentioned that l1 and l2 are easily confused with 11 and 12. The name
"arrays" could also be improved. Using a plural name for a single
object causes confusion; would an array of them be called "arrayses"?
And it's a structure, not an array. Pick a name that reflects what
it's used for, perhaps "struct line" or "struct line_info".
I declare functions like this : arrays *parseline(char *line, int N)
Given your original declaration, that would return a
pointer-to-pointer-to-structure. You seem to be throwing in '*'s
almost at random. Don't make something a pointer unless you have a
specific reason. (In fact, don't do *anything* unless you have a
specific reason.)

You can just return a structure directly:
arrays parseline(char *line, int N);
or
struct line_info parse_line(char *line, int n);
All-caps names are usually used for macros. Underscores are usually
used to separate words in identifiers; "parseline" is too easy to read
as a nonexistent 3-syllable word.
and I point to the arrays I want to return like this :

out.l1=displayl n;
out.l2=sumline;
out.Nval=Nval;
return out;


struct line_info parse_line(char *line, int n)
{
struct line_info result;
...
result.l1 = something;
result.l2 = something_else;
result.Nval = something_else_ again;
...
return result;
}

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
368
by: Dave A | last post by:
The following C code specifies the interface into a DLL. I need to access it from C#. How do I do declare it? I have done simple ones before but this particular API requires a pointer to a struct that contains an array of other structs. typedef struct { int nWidth; int nHeight; DWORD dwFlags; } HCA_MODE; typedef struct
7
7526
by: Rennie deGraaf | last post by:
A question regarding this code, which defines a struct containing a size and a variable-sized array: typedef struct { uint16_t count; unsigned char bytes; } foo_t; ....
5
3128
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;
17
3259
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
1
5661
by: CapMaster | last post by:
I've found some programs of how to create a standard game of blackjack on C++. But how would you do it using structs? Here is my assignment: Problem Statement: The purpose of this project is to create a game of Blackjack, which can be played by one player against the dealer (represented by the computer). The deck of cards is to be stored as an array of Card structures. Blackjack is played with a deck of 52 cards, consisting of four...
4
1943
by: =?Utf-8?B?a2lzaG9y?= | last post by:
Hi, has any one used webservices for returning custom objects other than datasets like custom classes and their internal classes ?. What problems you have faced if any ? is there any limitation ? what are advantages and disadvantages.. please let me know Kishor
7
1815
by: aemado | last post by:
I am trying to write a program that reads in data from a user-specified file. I have to sort this file two ways, one using parallel arrays and the other using structs. I then have to output the corrected files to the user. Each file contains up to 10 different sets of data, with each set containing four members. Such as this: Dell 512 1.7 120 HP 1024 1.4 80 Compaq 1024 1.6 100 Toshiba 512 1.8 160 Lenovo 2048 1.5 120 I have almost all...
2
1914
by: =?Utf-8?B?U2V0aEluTUk=?= | last post by:
I am a total newb at .net, and I have not been able to search out a best practice answer to what must be a common problem. My app must process binary data from a UDP socket, a MSMQ queue and a file. In C, the data is in nested structs, with mixed types, floats, ints, char arrays, int arrays, variable length arrays of structs etc. My preference would be to access the data in a similar fashion to C, casting the byte array of received...
160
5873
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
8831
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
9374
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
9249
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6796
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
6076
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
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.