Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[code]#include<stdio.h> typedef 1

Status
Not open for further replies.

satellite03

IS-IT--Management
Dec 26, 2003
248
IN
Code:
#include<stdio.h>

 typedef  struct book

 { int page;
   char author[20];
   float cost;

 };




int main()

{
  book b1(100,abcd,20.78);

  func(b1);

  printf(&quot;%d %d %d&quot;, b1.page,b1.author,b1.cost);

}


 func(book c )

{ c.page = 120;
  c.author=efgh;
  c.cost = 34.76;
}

hi this is a demo code . i wanted to implement structure....i am getting compilation error...where is the mistake ???
does structure are global variable???

i am also interested to send this structure by address and want to change the fields...how can i do that??
thanks

 
>.i am getting compilation error...where is the mistake ???

There are many errors in your code. Look at the following line by line and compare.

>i am also interested to send this structure by address and want to change the fields...how can i do that??

The following code shows this also.

Code:
#include <stdio.h>
#include <string.h>

typedef struct {
  int page;
  char author[20];
  float cost;
} book;


int main()

{
  book b1={100,&quot;abcd&quot;,20.78};

  func(&b1);

  printf(&quot;%d %s %d&quot;, b1.page,b1.author,b1.cost);

}

void func(book *c )

{ c->page = 120;
  strcpy(c->author,&quot;efgh&quot;);
  c->cost = 34.76;
}
 
Oops, 1 mistake I made:

Code:
printf(&quot;%d %s %d&quot;, b1.page,b1.author,b1.cost);
Should be:
Code:
printf(&quot;%d %s %f&quot;, b1.page,b1.author,b1.cost);
Or Possibly:
Code:
printf(&quot;%d %s %0.2f&quot;, b1.page,b1.author,b1.cost);

Also, you really should prototype func:
Code:
#include <stdio.h>
#include <string.h>

typedef struct {
  int page;
  char author[20];
  float cost;
} book;

void func(book *c);

int main()
.
.
.

That's what I get for being in a hurry.

Good Luck!
 
is that code really running????...i am getting error.

error
-------
Code:
Compiling...
ijkl.cpp
C:\ijkl\ijkl.cpp(16) : warning C4305: 'initializing' : truncation from 'const double' to 'float'
C:\ijkl\ijkl.cpp(18) : error C2065: 'func' : undeclared identifier
C:\ijkl\ijkl.cpp(22) : warning C4508: 'main' : function should return a value; 'void' return type assumed
C:\ijkl\ijkl.cpp(26) : error C2373: 'func' : redefinition; different type modifiers
C:\ijkl\ijkl.cpp(28) : warning C4305: '=' : truncation from 'const double' to 'float'
Error executing cl.exe.

ijkl.obj - 2 error(s), 3 warning(s)

i guessed it could be due to prototype error....so i used void func(book *c ); just after the header.. but still the compilation error is there.

and one more thing... &quot; strcpy(c->author,&quot;efgh&quot;);>>>> is there any other other way to do this???


 
oops sorry....i did not see your second post...thats why got error....probabily posts has gone simultaneously..

anyway, i changed accordingly and yes its running.

do we have to declare prototype just before main()????
bcoz i used void func(book *c ); just after the header and it was giving me 13 error!!!!!!!. but as soon as i declared just before main() all the error vanished.

and one more thing... &quot; strcpy(c->author,&quot;efgh&quot;);>>>> is there any other other way to do this???
thanks

 
> bcoz i used void func(book *c ); just after the header and it was giving me 13 error
Well since it refers to a book, it would at least need to be after the structure itself

The usual order is something like
Code:
#includes
#defines
struct, enum, typedef
function prototypes
global and static data
function bodies

--
 
> and one more thing... &quot; strcpy(c->author,&quot;efgh&quot;);>>>> is there any other other way to do this???

Yes, there are other ways to do this. You could use other functions such as memcpy or you could assign each individual array element as in:
Code:
  c->author[0] = 'e';
  c->author[1] = 'f';
  c->author[2] = 'g';
  c->author[3] = 'h';
  c->author[4] = '\0';
but with C, since there is no string assignment like s1=s2, strcpy would be the preferred method.
 
thank you salem for this IMPORTANT information.

&quot; Well since it refers to a book, it would at least need to be after the structure itself &quot; ...yes it is justified.

you have given some ordering...
Code:
#includes
#defines
struct, enum, typedef
function prototypes
global and static data
function bodies

..ordering is fine.
boz first we have to define data type(struct, enum, typedef etc ) then function prototype , then function body....its fine
u have given &quot;global and static data&quot; to be rank 5th.

is there any reason for this??

i think its not important to give it a rank 5th...i can give it any ranking....anyway its ok.


itsgsd thank you...i cleared my doubts
 
> is there any reason for this??
It has to be after struct's etc, because you may want a variable of a type which you've just created
It has to be after prototypes, because you may need to initialise a variable with a pointer to a function.
It has to be before function bodies, because these will refer to those variables.

Hence it is positioned neatly between all the declarations (typedefs, prototypes etc) and the definitions of functions.

I suppose you could be perverse and write
Code:
extern int foo;
void bar ( void ) {
   foo = 0;
}
int foo;
But all that buys you is a maintenance problem by adding one more foo to go mess with.

> i think its not important to give it a rank 5th...i can give it any ranking....anyway its ok.
But is it important to be consistent in your code. There is no point making life hard for yourself (and others) by being gratuitously inconsistent about how you layout your code.

--
 
>It has to be after prototypes, because you may need to initialise a variable with a pointer to a function.
......u are talking about function pointer.??

ok..suppose i am writing in a wrong order

static int var1;// static data

int* func(int , int ) // prototype


prototypes do not point to any particular data type......its just a declaration before the function...so even if i use this static int var1 in my function with a wrong order it should not complain....i do not know why u r talking abt pointer to a function.
 
sorry, i wanted to mean &quot;prototypes....any particular data &quot; not &quot; data type&quot;
 
An example
Code:
static void foo ( int a );  // a prototype

static void (*fn)(int) = foo;  // a function pointer, pointing to foo


--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top