|
Another very simple pointer question
Author |
Message |
Radith Silv #1 / 4
|
 Another very simple pointer question
I posted yesterday a pointer related question, I'm stuck again on this pointers business, very sorry, but can you please see whats wrong with this simple piece of code. What i want done is the array cubed through pointers. I know there's millions of ways to do this, but can you please keep it pointer related as I'm learning pointers. Thanx very very much; Radith Silva #include <stdio.h> void cube( int * ); int main() { int a[ 1 ] = { 3 }; cube( a ); printf( "%s", a ); return 0; Quote: }
void cube( int *ptr ) { *ptr = *ptr * *ptr * *ptr; Quote: }
|
Wed, 27 Oct 2004 11:18:52 GMT |
|
 |
Ben Pfaf #2 / 4
|
 Another very simple pointer question
Quote:
> int main() > { > int a[ 1 ] = { 3 }; > cube( a ); > printf( "%s", a );
Use %d to print an int argument. Quote: -- "...Almost makes you wonder why Heisenberg didn't include postinc/dec operators in the uncertainty principle. Which of course makes the above equivalent to Schrodinger's pointer..." --Anthony McDonald
|
Wed, 27 Oct 2004 11:26:02 GMT |
|
 |
Barry Schwar #3 / 4
|
 Another very simple pointer question
On Sat, 11 May 2002 13:18:52 +1000, "Radith Silva" Quote:
>I posted yesterday a pointer related question, I'm stuck again on this >pointers business, very sorry, but can you please see whats wrong with this >simple piece of code. >What i want done is the array cubed through pointers. I know there's >millions of ways to do this, but can you please keep it pointer related as >I'm learning pointers. >Thanx very very much; >Radith Silva >#include <stdio.h> >void cube( int * ); >int main() >{ > int a[ 1 ] = { 3 }; > cube( a ); > printf( "%s", a );
Here is your problem. The %s says that the next argument will be a string. a is not the address of a string, it is the address of an int. You don't want to print a string, especially since you don't have a string in your program. You want to print an int. The format for printing an int is %d. It requires an int argument in the parameter list. a is not an int, it is the address of an int. You want to print a[0]. To keep it pointer related, a[0] is the same as *(a+0) or *(a) or *a. Therefore: printf("%d\n", *a); Quote: > return 0; >} >void cube( int *ptr ) >{ > *ptr = *ptr * *ptr * *ptr; >}
<<Remove the del for email>>
|
Wed, 27 Oct 2004 12:52:02 GMT |
|
 |
Dan P #4 / 4
|
 Another very simple pointer question
Quote: >I posted yesterday a pointer related question, I'm stuck again on this >pointers business, very sorry, but can you please see whats wrong with this >simple piece of code. >What i want done is the array cubed through pointers. I know there's >millions of ways to do this, but can you please keep it pointer related as >I'm learning pointers. >#include <stdio.h> >void cube( int * ); >int main() >{ > int a[ 1 ] = { 3 };
Don't declare an array, if all you need is a pointer. int a = 3; /* it is enough to declare a scalar */ Quote: > cube( a );
cube(&a); /* pass the address of our scalar to the function */ Quote: > printf( "%s", a );
printf("%d\n", a); Quote: > return 0; >} >void cube( int *ptr ) >{ > *ptr = *ptr * *ptr * *ptr; >}
The only technical error in your original example is in the printf call. To be compatible with the rest of your code, it should look like this: printf("%d\n", a[0]) Note that you've got both printf arguments wrong, so you should get more familiar with the simple usage of this function before proceeding with pointer issues. Your example is flawed in its approach: you don't declare arrays without a good reason and there is no such reason in your program, where all you need is a scalar. In real applications you don't want to use this kind of function interfaces: they're evil. Not only the code is less readable than it should be, but they have a very high potential for generating hard to trace bugs. Dan -- Dan Pop DESY Zeuthen, RZ group
|
Fri, 29 Oct 2004 20:06:55 GMT |
|
|
|