LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   question about calling an array inside of a user defined function? (http://www.linuxquestions.org/questions/programming-9/question-about-calling-an-array-inside-of-a-user-defined-function-4175470782/)

lleb 07-24-2013 02:38 PM

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)
{
        int i;
        printf("Please enter %d integer numbers separated by spaces:\n", size);
        for (i = 0; i < size; i++)
                scanf("%d", &A[i]);
}

so nothing out of the ordinary there. that should be a standard for loop and use of scanf, sadly prof has not covered ssanf or any of the other options so i am stuck using scanf for now. maybe someday down the line in a other program or after this course ill get the chance to learn about better options for gathering data from the user.

im confused as to my next function:

Code:

void SearchArray(int A[], int target, int size);
ive not written any code here yet as im not sure if i should call the A[], or *A for the first type of the function?

If i call *A do i then use something like this for my search:

Code:

for (*A = 0; *A < size; *A++)
if (*A < target)

or use A[] insteadA?

Code:

for (i = 0; i < size; i++)
if (A[i] = target)

thanks in advance.

serafean 07-24-2013 05:12 PM

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

johnsfine 07-24-2013 05:59 PM

Quote:

Originally Posted by lleb (Post 4996050)
Code:

void SearchArray(int A[], int target, int size);
ive not written any code here yet as im not sure if i should call the A[], or *A for the first type of the function?

There is no difference in meaning between
Code:

void SearchArray(int A[], int target, int size);
and
Code:

void SearchArray(int *A, int target, int size);
Use whichever you prefer, and your choice for which you use does not need to affect any of the other code involving A.
Quote:

If i call *A do i then use something like this for my search:

Code:

for (*A = 0; *A < size; *A++)
if (*A < target)

or use A[] insteadA?
Your use of *A++ should generate a compiler warning, maybe even an error. The * in that accomplishes nothing. More seriously, your comparison to size is wrong. You want to compare an index with size (or compare pointers), not compare the contents pointed to with size. Also *A=0 is doing something entirely different than you intend.

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:

Code:

for (i = 0; i < size; i++)
if (A[i] = target)


Much closer. But a single = is always assignment. You need == for comparison.

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:

Originally Posted by serafean (Post 4996150)
I admit I don't know the difference between declaring a function with A[] or *A as its parameter.

In other places in C code there is a big difference between declaring A[] and declaring *A. But for declaration of a function parameter, there is no difference.

lleb 07-24-2013 06:08 PM

thank you both. that is good information. ill be working on that when i get home.

johnsfine 07-25-2013 07:36 AM

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++)
  if ( *A == target )
      ...

If your compiler is set to enforce an older dialect of C, that would be:
Code:

int *stop;
...
for (stop=A+size; A<stop; A++)
  if ( *A == target )
      ...


psionl0 07-25-2013 12:41 PM

Quote:

Originally Posted by johnsfine (Post 4996173)
In other places in C code there is a big difference between declaring A[] and declaring *A. But for declaration of a function parameter, there is no difference.

I'm not sure that is true since in neither case you are allocating memory for A.

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.

johnsfine 07-25-2013 01:37 PM

Quote:

Originally Posted by psionl0 (Post 4996626)
I'm not sure that is true

I have no idea what part of my statement you are disagreeing with.

Quote:

since in neither case you are allocating memory for A.
Nor which two cases are implied by that "neither".

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.
You are giving an example to support the first part of my statement. Does that mean it was the second part you disagree with?

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[];
or
Code:

extern int *A;
There is a big difference between those two. For the first case, the other module needs to have declared A so it actually IS the constant address of int that you want here. But for the second case, the other module needs to have declared A as a pointer CONTAINING the (maybe variable) address of int that you want here.

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[])
or
Code:

void fun(int *A)
There is no difference between the two.

psionl0 07-26-2013 06:59 AM

Quote:

Originally Posted by johnsfine (Post 4996649)
I have no idea what part of my statement you are disagreeing with.



Nor which two cases are implied by that "neither".

I don't know what's not to understand, especially since I quoted the part of your post that I had a problem with.

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:

Originally Posted by johnsfine (Post 4996649)
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[];
or
Code:

extern int *A;

That is an example where it makes a difference. A[] would almost certainly be considered a constant pointer if it is declared as external. Of course, if the module that declared A[] globally didn't allocate memory for it then that should result in a linker error.


All times are GMT -5. The time now is 05:04 PM.