question about calling an array inside of a user defined function?
im tasked with creating a linear search script using functions on a 10 element array. the elements are to be supplied by the user as is the search target.
I understand how to create the array and gather that information from the user as well as howto set a variable for "target", this is what im calling it. Those two parts are simple enough. I am not fully understanding the calling of an array in a function as a pointer. i semi understand the use of a pointer and howto call a normal pointer in a function. i also understand that an array is nothing more then a "special" pointer with a set of consecutive address blocks for the size of the array. THIS IS HOMEWORK so please no exact code, just examples. my first user defined function is simple enough: Code:
ReadArray(int A[], int size) im confused as to my next function: Code:
void SearchArray(int A[], int target, int size); If i call *A do i then use something like this for my search: Code:
for (*A = 0; *A < size; *A++) Code:
for (i = 0; i < size; i++) |
Hi,
[] is called the subscript operator. It allows you to access a variable at a position in an array. * is called the dereference operator. It allows you to access the item pointed to by the pointer you are dereferencing. You can access arrays using both. either by subscripting the item you want : A[3] Or using pointers and offsets and dereferencing it : *(A + 3) Both of these mean "give me the item at the fourth position" (becacuse we're indexing from 0 ) I admit I don't know the difference between declaring a function with A[] or *A as its parameter. I suggest you read a bit about how C manages memory |
Quote:
Code:
void SearchArray(int A[], int target, int size); Code:
void SearchArray(int *A, int target, int size); Quote:
You could code that loop, incrementing a pointer rather than using indexes. But you have badly missed in the attempt. Giving you the right code would be easy; Telling you how to write it yourself, harder. Maybe you should wait for an example in which that approach fits better. Meanwhile: Quote:
In answer to your main question, the choice of declaring the function parameter int *A vs int A[] is completely independent of the choice of accessing it with *A vs. A[i] You can use either declaration syntax with either access syntax. The problem with accessing via *A in your function is that it makes the proper use of the size parameter a bit less obvious (not difficult once you know those things, just a bit less obvious for now). Quote:
|
thank you both. that is good information. ill be working on that when i get home.
|
Hopefully you tried some more yourself and got the learning benefit of that. So I can show an example of the other approach so you will know it exists (for other situations where it might be better). In this case I think accessing A[i] is more readable but:
Code:
for (int *stop=A+size; A<stop; A++) Code:
int *stop; |
Quote:
There is a big difference between: int A[100]; and int *A = malloc(100*sizeof(int)); since the former would be a constant pointer but not the latter. |
Quote:
Quote:
Quote:
Let me give two examples that are more specific to the question: 1) Assume A (the address of some integers) has been declared in a different module and you refer to it in the current module with one of these: Code:
extern int A[]; Code:
extern int *A; 2) Assume A (somehow defined as an address of int) will be passed to your function which you declare as either Code:
void fun(int A[]) Code:
void fun(int *A) |
Quote:
If an array is declared in either method but no memory is allocated (whether on the stack, data section or heap) then it makes no sense to assume it is a constant pointer since it would either be NULL or point to an invalid area. Quote:
|
All times are GMT -5. The time now is 07:24 AM. |