Re: "Pointers" and "by reference" do my head in!
From: Nils Petter Vaskinn (me_at_privacy.net)
Date: 04/13/04
- Next message: Anand Hariharan: "Re: do anyboby know how to convert binary to decimal and hexadecimal to decimal"
- Previous message: Karl Heinz Buchegger: "Re: "Pointers" and "by reference" do my head in!"
- In reply to: Ian Dennison: ""Pointers" and "by reference" do my head in!"
- Next in thread: Ian Dennison: "Re: "Pointers" and "by reference" do my head in!"
- Reply: Ian Dennison: "Re: "Pointers" and "by reference" do my head in!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Anand Hariharan: "Re: do anyboby know how to convert binary to decimal and hexadecimal to decimal"
- Previous message: Karl Heinz Buchegger: "Re: "Pointers" and "by reference" do my head in!"
- In reply to: Ian Dennison: ""Pointers" and "by reference" do my head in!"
- Next in thread: Ian Dennison: "Re: "Pointers" and "by reference" do my head in!"
- Reply: Ian Dennison: "Re: "Pointers" and "by reference" do my head in!"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|