Re: "Pointers" and "by reference" do my head in!

From: Nils Petter Vaskinn (me_at_privacy.net)
Date: 04/13/04


Date: Tue, 13 Apr 2004 19:29:01 +0200

On Tue, 13 Apr 2004 17:04:23 +0000, Ian Dennison wrote:

> (New to C, not to Programming!) Environment = gcc 3.3.2 Solaris 9 (Sparc)

It might help if you told us what you have been programmng brfore,
that way we coul have explained things in the terms you are used to
from before.

> OK, situation as follows, I am passing an array of structures to a
> procedure, and also passing the counter for the element in the array I am
> dealing with. I increment the counter inside the function, but the value
> stays the same outside. I have searched my K & R book and C Study Notes and
> Google for keywords "pass by reference" but now I am stumped! Whats the
> "keyword" for this type of functionality that I can search on or research?
> Or am I going to have to bite the bullet and grapple with pointers?
>
> here's my test script to prove the problem,....
>
> #include <stdio.h>

/* this function takes an int as its argument, that integer exists
 * only inside this function */
> void test_function(int intCounter)
> {

/* Modify that integer */
> intCounter++;
>
> printf(" in function - intCounter=%i\n", intCounter);
>
> }
>

> main()
> {

/* make an integer and give it the value 5 */
> int intCounter=5;
>
> printf("intCounter=%i\n", intCounter);
>

/* Call the function, which gets a copy of the integer */
> test_function(intCounter);

/* The integer we have here stays the same */
> printf("intCounter=%i\n", intCounter);
>
> }

/* now for one way it can be done: (untested code) */

#include <stdio.h>
/* A function that takes a pointer to an integer */
void foo(int *p)
{
    /* Modify the integer that p points to */
    (*p)++;
    /* Print the integer that p points to */
    printf("The int that p points to is %d\n", *p);
}

int main()
{
    /* make an integer and give it the value 5 */
    int intCounter=5;

    printf("intCounter=%i\n", intCounter);

    /* Call the function, whith the address of the integer (a pointer
     * to the integer */
    foo(&intCounter);

    /* Print the modified integer */
    printf("intCounter=%i\n", intCounter);

}
  

-- 
NPV
What did that old blonde gal say? -- That is the part you throw away.
   Tom Waits - The part you throw away


Relevant Pages

  • Re: Malloc code
    ... int xxx; ... As for not using the void pointer, I will have to do some further testing ... I just needed some insight on passing arrays of pointers. ... struct MCB *r1; ...
    (microsoft.public.vc.language)
  • Re: Using a link list over an array.
    ... compile (p->data is a void *) so you have not shown us some key ... You can't use it to sort a list of ints where the int is ... You can use it to sort a list of pointers to any type. ...
    (comp.lang.c)
  • Re: void *
    ... list_add_head(&p, (int *)100); ... That was my understanding of 'void *' concept. ... In fact at your level the first thing you should assume when you appear to need a cast is that you have done something wrong and a cast is *not* the solution. ... Personally I don't think you understand the concept of pointers yet, so you need to read the sections of your text book and the comp.lang.c FAQ that refer to pointers. ...
    (comp.lang.c)
  • Re: How are C++ objects laid out in memory ?
    ... > void print(); ... int main ... > But the function pointers declared in above MsgStruct structure have ... > pointers which I can locate and then invoke the function pointers? ...
    (comp.lang.asm.x86)
  • for my effort: GC (and Dynamic Types), General Spec
    ... it would be included in the gc header and, in truth, a simply 'void *' pointer. ... Pointers may be freely stored on the stack, in global variables, or in other garbage-collected objects. ... int gctypep; ... effort should be used to ensure that the string does not clash with a builtin type or one provided by another library. ...
    (comp.std.c)