How to make my formal arguments use pointer notation

Started by
7 comments, last by jpetrie 16 years, 10 months ago
My working code so far:

#include <stdlib.h>
#include <stdio.h>

void print(int []);

int main()
{
        int A[5] = {1, 2, 3, 4, 5};
        print(A);
        return 0;
}

void print(int x[])
{
        printf("%d", *(x+0));
}


I've been asked to write some code where the "actual arguments" to the function will be the name of the array and the "formal argument" will use pointer notation. Also the code in the function is to use pointer notation instead of array notation. As you can see in my function, I know how to use pointer notation. My question is twofold. Do "formal arguments" refer to the argument list in the function definition? And if so, how do I modify my current argument list "int x[]" so that it uses pointer notation. I'm not sure if I'm suppoesd to do it like 'void print(&x)' or 'void print(int*x[])' or what. Note: This is not a homework problem. I'm working through a tutorial in a book and have come across this issue. I can easily program this to do whatever is asked of me, but I'd like to do it exactly the way it's asking me to in order to make sure that I understand pointers properly. Thanks. Oh and my current working guess is that my function definition would start: 'void print(int *x)'
Everyone hates #1.That's why a lot of idiots complain about WoW, the current president, and why they all loved google so much when it was new.Forget the fact that WoW is the greatest game ever created, our president rocks and the brainless buffons of America care more about how articulate you are than your decision making skills, and that google supports adware, spyware, and communism.
Advertisement
"*(x+0)" is a REALLY bizarre way of saying you know "pointer notation".

You've claimed this isn't homework, so I can give you a conclusive answer:

void print(int *x){  printf("%d", x[0]);}


This will work without changing your 'main' function at all.

However, there's no guarantee that x is actually a pointer to an array. You could pass in a pointer to a single int. What happens if you then print out x[1]? It prints the value of the next integer sized chunk of memory, which is effectively an unknown. C/C++ will let you do this anyway, and it's the standard way of doing it if you don't use a container class or typical array.


Why does this work? Because C and C++'s array operations are nothing but slightly easier to read pointer operations.
Your function already takes a pointer.

C++ arrays decay to pointers when passed to functions (and in certain other operations too). All you would be doing is changing what you see, not what the compiler sees.
Quote:Original post by Nypyren
"*(x+0)" is a REALLY bizarre way of saying you know "pointer notation".

You've claimed this isn't homework, so I can give you a conclusive answer:

void print(int *x){  printf("%d", x[0]);}


This will work without changing your 'main' function at all.

However, there's no guarantee that x is actually a pointer to an array. You could pass in a pointer to a single int. What happens if you then print out x[1]? It prints the value of the next integer sized chunk of memory, which is effectively an unknown. C/C++ will let you do this anyway, and it's the standard way of doing it if you don't use a container class or typical array.


Why does this work? Because C and C++'s array operations are nothing but slightly easier to read pointer operations.


Ok so my guess was correct. Thank you. I think your x[0] is incorrect though. I stated that the code in the function needed to use pointer notation which would be *(x+0)

And sorry I wasn't more clear. I didn't mean to say I was completely familiar with pointer notation. I just meant I could use pointer notation to access arrays. aka *(arrayName + index)
Everyone hates #1.That's why a lot of idiots complain about WoW, the current president, and why they all loved google so much when it was new.Forget the fact that WoW is the greatest game ever created, our president rocks and the brainless buffons of America care more about how articulate you are than your decision making skills, and that google supports adware, spyware, and communism.
so, here is nice tutorial about pointers

EDIT:
Brother Bob: uhm, you're right (so i erased that part)
seems like i'm the one who should go through that tutorial (again)
rip-off: hm, ok, i better shut up..
i hope at least that tutorial is nice :/

[Edited by - psycho_svk on June 8, 2007 5:09:28 AM]
Quote:Original post by psycho_svk
you should not use *(arrayName + index) for accesing arrays (its ugly, not practical, and there are better ways: using [])
arrayName is pointer (address of the first value in your array)
but integer variables are 4 bytes long
so to access the second value in your array, you need this: *(arrayName + 4)
and with index it would be *(arrayName + 4*index)

Nope. Pointer arithmetics automatically adjusts for the size of the object the pointer points to. *(arrayName + 1) refers to the second item, no matter what the size of the object being pointer to is.
Quote:Original post by Brother Bob
Quote:Original post by psycho_svk
you should not use *(arrayName + index) for accesing arrays (its ugly, not practical, and there are better ways: using [])
arrayName is pointer (address of the first value in your array)
but integer variables are 4 bytes long
so to access the second value in your array, you need this: *(arrayName + 4)
and with index it would be *(arrayName + 4*index)

Nope. Pointer arithmetics automatically adjusts for the size of the object the pointer points to. *(arrayName + 1) refers to the second item, no matter what the size of the object being pointer to is.


Adding to that, an array is *not* a pointer. It only decays to one in certain situations (one of these, as I mentioned, is when an array is passed to a function).

A very obvious difference is that sizeof(arrayname) gives the total number of bytes in an array, whereas sizeof(pointer) is the same regardless of whether it is pointing to an array or not. When you pass an array to a function using the bracket notation "int foo( int array[] )" the array size information is "lost" due to the pointer decay, so sizeof(array) there is the same as sizeof(int *).

So these statements are wrong:
Quote:
make array of 3 ints, its address is in 'a'
now you can access your array with both pointers, they are the same


This too is wrong
Quote:
but integer variables are 4 bytes long


Integer variables may or may not be 4 bytes long. They are actually sizeof(int) bytes long. It just happens that the majority of consumer PCs available were 32 bit machines. You are now living in the time when 64bit machines will become common. The first step to such an architecture is to undo such assumptions, particularly about pointer sizes.

Don't worry about making mistakes psycho_svk, its how everyone here learned these things [smile]. IMO the pointer/array difference issues aren't properly highlighted in much introductory C++ text, only their similarities. This leads to the incorrect conclusions that you (and I, once [grin]) held.

[Edited by - rip-off on June 8, 2007 5:11:03 AM]
thx for corrections, i'll be quiet now..
Quote:
I stated that the code in the function needed to use pointer notation which would be *(x+0)

Except in the presence of overload operator[], foo[bar] is pure syntactic sugar for *(foo + bar) (in other words, it's exactly the same). The only reason it is ever really differentiated is in homework assignments where the point is drive to home that concept that foo[bar] and *(foo + bar) are equivalent methods of "pointer notation."

That's why accessing arrays by constant integer literal like arr[5] is the same as 5[arr] (again, assuming no overloading).

This topic is closed to new replies.

Advertisement