Re: real newbie question on pointers



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 address

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 ()

don't do this with your braces---
{ 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;

{
*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?
It's old K&R C which is sometimes indigestible by today's compilers.

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.
.



Relevant Pages

  • Re: K&R2 ex 1-8
    ... and it's best to use more blanks (after commas and around ... int main ... If an "if" or other control statement controls only a single ... the braces associated with the "while" are unnecessary. ...
    (comp.lang.c)
  • Re: [PATCH 1/4] Blackfin: arch patch for 2.6.18
    ... +int dma_channel_active ... Drop all of those braces. ... Don't indent labels so much. ... +sys_ipc(uint call, int first, int second, int third, void *ptr, long fifth) ...
    (Linux-Kernel)
  • Using Get() and Set() instead of accessing the variable directly
    ... I feel as though accessing variables directly is just bad practice. ... I can access private/protected variables, but when I divide up the class ... int m_x; ... I don't know if the compiler treats it as accessing the variable directly or ...
    (microsoft.public.vc.mfc)
  • Re: Valid C syntax.
    ... int  a,b,c; ... You should not over-think what the compiler will do. ... to specifications. ... In practice, I would accomplish the above in two statements if there ...
    (comp.lang.c)
  • Re: [RFC][PATCH 9/13] Equinox SST driver: tty interface
    ... +extern int eqnx_modem; ... no parens (multiple places) ... Don't use braces for 1-statement blocks. ...
    (Linux-Kernel)