Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > #include problem

Reply
Thread Tools

#include problem

 
 
arnuld
Guest
Posts: n/a
 
      10-07-2011
I am running into some issue of inclusion of header and I am unable to
understand what is wrong:



fileA.h

#ifndef TEST_HEADERS_A
#define TEST_HEADERS_A

struct myStruct_A
{
int num;
};


void use_print_values(void);

#endif




fileB.h

#ifndef TEST_HEADERS_B
#define TEST_HEADERS_B

#include "fileA.h"

void print_values(struct s1 s);

#endif




fileA.c

#include <stdio.h>
#include "fileA.h"
#include "fileB.h"

void use_print_values(void)
{
struct myStruct_A s;
print_values(s);
}




fileB.c

#include <stdio.h>
#include "fileA.h"
#include "fileB.h"

void print_values(struct myStruct_A s)
{
printf("Num = %d\n", s.num);
}




==================== OUTPUT ======================
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra -c fileA.c
In file included from fileA.c:3:
fileB.h:6: warning: ‘struct s1’ declared inside parameter list
fileB.h:6: warning: its scope is only this definition or declaration,
which is probably not what you want
fileA.c: In function ‘use_print_values’:
fileA.c:8: error: type of formal parameter 1 is incomplete
[arnuld@dune C]$


[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra -c fileB.c
In file included from fileB.c:3:
fileB.h:6: warning: ‘struct s1’ declared inside parameter list
fileB.h:6: warning: its scope is only this definition or declaration,
which is probably not what you want
fileB.c:6: error: conflicting types for ‘print_values’
fileB.h:6: error: previous declaration of ‘print_values’ was here
[arnuld@dune C]$





--
arnuld
www.LispMachine.Wordpress.com
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      10-07-2011
On 2011-10-07, arnuld <(E-Mail Removed)> wrote:
> I am running into some issue of inclusion of header and I am unable to
> understand what is wrong:
>
> [sni]
>
> fileB.h
>
> #ifndef TEST_HEADERS_B
> #define TEST_HEADERS_B
>
> #include "fileA.h"
>
> void print_values(struct s1 s);


Did you mean "struct myStruct_A s" here?

>
> #endif

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      10-07-2011
arnuld <(E-Mail Removed)> writes:

> I am running into some issue of inclusion of header and I am unable to
> understand what is wrong:


It's not inclusion that is the problem.

> fileA.h
>
> #ifndef TEST_HEADERS_A
> #define TEST_HEADERS_A
>
> struct myStruct_A
> {
> int num;
> };
>
>
> void use_print_values(void);
>
> #endif
>
>
>
>
> fileB.h
>
> #ifndef TEST_HEADERS_B
> #define TEST_HEADERS_B
>
> #include "fileA.h"
>
> void print_values(struct s1 s);


struct s1 is not defined. Presumably you intended to refer to the
structure defined in fileA.h but that struct myStruct_A.

> #endif


<snip>
--
Ben.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      10-07-2011
> On Fri, 07 Oct 2011 12:34:22 +0000, Ike Naar wrote:

>> On 2011-10-07, arnuld <(E-Mail Removed)> wrote:


> Did you mean "struct myStruct_A s" here?


Eh.... confused me :\




--
arnuld
http://LispMachine.Wordpress.com
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      10-07-2011
> On Fri, 07 Oct 2011 13:38:54 +0100, Ben Bacarisse wrote:

> It's not inclusion that is the problem.


fileA.h was incomplete, here it is:

#ifndef TEST_HEADERS_A
#define TEST_HEADERS_A

#include "fileB.h"

struct myStruct_A
{
int num;
};


void use_print_values(void);

#endif

============ OUTPUT ==============
[arnuld@dune C]$ gcc -ansi -pedantic -Wall -Wextra -c fileB.c
In file included from fileA.h:4,
from fileB.c:2:
fileB.h:6: warning: ‘struct myStruct_A’ declared inside parameter list
fileB.h:6: warning: its scope is only this definition or declaration,
which is probably not what you want
fileB.c:6: error: conflicting types for ‘print_values’
fileB.h:6: error: previous declaration of ‘print_values’ was here
[arnuld@dune C]$



Header A asks for Header B and Header B asks for Header A, recursive
issue, anyway to solve this ?




--
arnuld
http://LispMachine.Wordpress.com
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      10-08-2011
"arnuld" <(E-Mail Removed)> wrote in message
news:4e8ef020$0$287$(E-Mail Removed)...
> #ifndef TEST_HEADERS_A
> #define TEST_HEADERS_A
>
> struct myStruct_A
> {
> int num;
> };
>


Sorry for asking additional questions instead of answering yours: I often
observe the structures containing a single member, often that is an array.
What is the reason for doing so?

Mark


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-08-2011
On 10/ 9/11 07:53 AM, Mark wrote:
> "arnuld"<(E-Mail Removed)> wrote in message
> news:4e8ef020$0$287$(E-Mail Removed)...
>> #ifndef TEST_HEADERS_A
>> #define TEST_HEADERS_A
>>
>> struct myStruct_A
>> {
>> int num;
>> };
>>

>
> Sorry for asking additional questions instead of answering yours: I often
> observe the structures containing a single member, often that is an array.
> What is the reason for doing so?


So the array can be copied with one assignment.

--
Ian Collins
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      10-09-2011

"Ian Collins" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 10/ 9/11 07:53 AM, Mark wrote:
>> "arnuld"<(E-Mail Removed)> wrote in message
>> news:4e8ef020$0$287$(E-Mail Removed)...
>>> #ifndef TEST_HEADERS_A
>>> #define TEST_HEADERS_A
>>>
>>> struct myStruct_A
>>> {
>>> int num;
>>> };
>>>

>>
>> Sorry for asking additional questions instead of answering yours: I
>> often
>> observe the structures containing a single member, often that is an
>> array.
>> What is the reason for doing so?

>
> So the array can be copied with one assignment.


But what exactly do you mean by "can be copied with one assignment" ?

struct my
{
unsigned int foo[6];
};

struct my a1 = { {0xa, 0xb, 0xc, 0x1, 0x2, 0x3} };
struct my a2;

memcpy(&a1, &2, sizeof(struct my));

I still need memcpy, like I would for copying arrays.

Mark


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-09-2011
On 10/ 9/11 04:20 PM, Mark wrote:
> "Ian Collins"<(E-Mail Removed)> wrote:
>> On 10/ 9/11 07:53 AM, Mark wrote:
>>>
>>> Sorry for asking additional questions instead of answering yours: I
>>> often
>>> observe the structures containing a single member, often that is an
>>> array.
>>> What is the reason for doing so?

>>
>> So the array can be copied with one assignment.

>
> But what exactly do you mean by "can be copied with one assignment" ?
>
> struct my
> {
> unsigned int foo[6];
> };
>
> struct my a1 = { {0xa, 0xb, 0xc, 0x1, 0x2, 0x3} };
> struct my a2;
>
> memcpy(&a1,&2, sizeof(struct my));
>
> I still need memcpy, like I would for copying arrays.


No, just write

a2 = 1a;

--
Ian Collins
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      10-09-2011
On 10/08/2011 11:32 PM, Ian Collins wrote:
> On 10/ 9/11 04:20 PM, Mark wrote:

....
>> But what exactly do you mean by "can be copied with one assignment" ?
>>
>> struct my
>> {
>> unsigned int foo[6];
>> };
>>
>> struct my a1 = { {0xa, 0xb, 0xc, 0x1, 0x2, 0x3} };
>> struct my a2;
>>
>> memcpy(&a1,&2, sizeof(struct my));


memcpy(&a2, &a1, sizeof a2);

>> I still need memcpy, like I would for copying arrays.

>
> No, just write
>
> a2 = 1a;


a2 = a1;
--
James Kuyper
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments