Re: real newbie question on pointers
- From: Geoff <geoff@xxxxxxxxxxxxxxx>
- Date: Tue, 31 Mar 2009 16:40:17 -0500 (CDT)
Where to begin...
The example is full of typos and bad usage, apparently from the
conversion to a web page. This is also very old C practice, the K&R
style which permitted forward references to functions without
prototypes. What you are looking at is pre-ANSI C that has a few
typographical errors in it.
On Mon, 30 Mar 2009 13:01:20 -0500 (CDT), Samantha
<samantha@xxxxxxxxxxx> wrote:
web site addressdon't do this with your braces---
http://www.iu.hio.no/~mark/CTutorial/CTutorial.html#Preface
C Programming Tutorial (K&R version 4)
Variable Parameters
code example:
/**************************************/
/* Pointer and function example 1 */
/* pnfex1a.c */
/**************************************/
#include <stdio.h>
main ()
{ int i,j;
GetValues (&i,&j);
printf ("i = %d and j = %d",i,j)
}
/************************************/
Below, because of sloppy braces, you have a function definition
without a body. This function needs a prototype or needs to be defined
before any function can call it. The authors' brace usage is
inconsistent and leads to errors. K&R C prescribed the opening brace
remain on the line of the function declaration as shown in corrected
versions below.
GetValues (p,q)These are supposed to be in the body so they belong in the braces but
this was K&R practice pre-ANSI and now they are defined in the
function declaration itself.
int *p,*q;It's old K&R C which is sometimes indigestible by today's compilers.
{
*p = 10;
*q = 20;
}
gcc compiler output:
pnfex1a.c: In function 'main':
pnfex1a.c:14: error: parse error before '}' token
pnfex1a.c:23: error: 'p' undeclared (first use in this function)
pnfex1a.c:23: error: (Each undeclared identifier is reported only once
pnfex1a.c:23: error: for each function it appears in.)
pnfex1a.c:24: error: 'q' undeclared (first use in this function)
pnfex1a.c:25: error: parse error at end of input
what is wrong with this code?
mac osx 10.4.11 tiger gcc compiler in terminal.
I don't understand why I am getting these compile errors when I go to
compile this code. All the other examples in the tutorial worked fine?
ANY idea's would be appreciated... THANKS SO MUCH!!!
S.
Function main is always defined as:
int main(int argc, char* argv[])
{
}
even if you never use the arguments it receives.
So, to fix it up and make it work in GCC:
/**************************************/
/* Pointer and function example 1 */
/* pnfex1a.c */
/**************************************/
#include <stdio.h>
void GetValues (int *p, int *q) {
*p = 10;
*q = 20;
}
int main(int argc, char* argv[]) {
int i,j;
GetValues (&i,&j);
printf ("i = %d and j = %d",i,j);
}
If you prefer to keep main at the top of the program then you must
write a prototype for GetValues at the top as:
void GetValues (int *p, int *q);
Your program then looks like this:
/**************************************/
/* Pointer and function example 1 */
/* pnfex1a.c */
/**************************************/
#include <stdio.h>
void GetValues (int *p, int *q);
int main(int argc, char* argv[]) {
int i,j;
GetValues (&i,&j);
printf ("i = %d and j = %d",i,j);
}
void GetValues (int *p, int *q) {
*p = 10;
*q = 20;
}
--
comp.lang.c.moderated - moderation address: clcm@xxxxxxxxxxxx -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
.
- Prev by Date: Re: string in an array
- Next by Date: Re: real newbie question on pointers
- Previous by thread: Re: real newbie question on pointers
- Next by thread: Re: real newbie question on pointers
- Index(es):
Relevant Pages
|