Can someone tell me why this won't compile?(to do with strings, arrays, pointers)

Started by
15 comments, last by tiresandplanes 11 years, 2 months ago

If you're starting fresh - why start with c? I'd recommend you jump straight to c++ or c#.

And no - you don't need to know c to learn c++.

Advertisement

Withdrawn due to errors on my part.

For some reason this won't compile. This isn't the first time that something hasn't worked from this book so I suspect it's a tiny typo that is messing up the whole thing. Can someone explain to me why this doesn't compile? I'm just starting to deal with strings so I don't know how to call a function from within a string.


#include <stdio.h>

main ()

{ int str_number;

for (str_number = 0; str_number < 13; str_number++)
   {
   printf ("%s", menutext(str_number));
   }
}

/*********************************************************/

char *menutext(n)
int n;

{
  static char *t[] =
   {
   "  -------------------------------------- \n",
   " |            ++ MENU ++                |\n",
   " |           ~~~~~~~~~~~~               |\n",
   " |     (1) Edit Defaults                |\n",
   " |     (2) Print Charge Sheet           |\n",
   " |     (3) Print Log Sheet              |\n",
   " |     (4) Bill Calculator              |\n",
   " |     (q) Quit                         |\n",
   " |                                      |\n",
   " |                                      |\n",
   " |     Please Enter Choice              |\n",
   " |                                      |\n",
   "  -------------------------------------- \n"
   };
return (t[n]);
}



I get an error on line 15: char *menutext(n)

C:\Documents and Settings\Gary II\Desktop\cprog.c|15|error: conflicting types for 'menutext'|

char *menutext(n) um... where's your ;

I'm sure other people have their points too, but that was the first thing that cought my eye, and the compiler did say... that line was causing you the problem.

As already mentioned, his code is a valid C90 function definition written in K&R style. It's not a variable.

Withdrawn due to errors in my part.

His compiler reads it just fine. The error says "conflicting types for 'menutext'". It's complaining that menutext was implicitly declared to return int, but is defined to return char *.

I'm not saying that anyone should use K&R style C, but that's not the cause of the problem here.

His compiler reads it just fine. The error says "conflicting types for 'menutext'". It's complaining that menutext was implicitly declared to return int, but is defined to return char *.

I'm not saying that anyone should use K&R style C, but that's not the cause of the problem here.

This I can accept. Thank you sir. As stated before, you are a well inteligent human being and I'm glad I learned something from you. smile.png

Also anyone that reads this and needs a good c reference online that free and up-to-date I've found http://publications.gbdirect.co.uk/c_book/ to be a great help.

This topic is closed to new replies.

Advertisement