Newbie question: Pointer to an array and passing it into a function 
Author Message
 Newbie question: Pointer to an array and passing it into a function

Hi

I have a large array of unsigned long and I want to populate the array
within a function.  Rather than passing the entire array into the
function, I want to just pass a pointer to the array into the
function, and then be able to load the data into the array from there.

int main(int argc, char *argv[]){

unsigned long address;
unsigned long wordcount;
unsigned long memory[MEMORY_SIZE], *p;

p = &memory[0];

.
.
.

loader(*p);

Quote:
}

void loader(unsigned long *p){
//Load data into array

???

Quote:
}

I am not sure if I've setup the pointer correctly.  Also once in the
function how do I access the different array elements???

Greatly Appreciated
Areef Kassam



Wed, 10 Mar 2004 10:32:08 GMT  
 Newbie question: Pointer to an array and passing it into a function

Quote:

> Hi

> I have a large array of unsigned long and I want to populate the array
> within a function.  Rather than passing the entire array into the
> function, I want to just pass a pointer to the array into the
> function, and then be able to load the data into the array from there.

> int main(int argc, char *argv[]){

int main(void)
{

Quote:
> unsigned long address;
> unsigned long wordcount;
> unsigned long memory[MEMORY_SIZE], *p;

> p = &memory[0];

or
    p = memory;

Quote:

> .
> .
> .

> loader(*p);

should be
    loader(p);
p is a pointer value.
*p is a long unsigned value.
could even be
    loader(memory);

Quote:
> }

> void loader(unsigned long *p){
> //Load data into array

> ???

> }

> I am not sure if I've setup the pointer correctly.  Also once in the
> function how do I access the different array elements???

p[n] = address;
wordcount = p[n];

--
 pete



Wed, 10 Mar 2004 11:44:28 GMT  
 Newbie question: Pointer to an array and passing it into a function


Quote:
> Hi

> I have a large array of unsigned long and I want to populate the array
> within a function.  Rather than passing the entire array into the

Which is not possible in C, because you always pass the address of it.

Quote:
> function, I want to just pass a pointer to the array into the
> function, and then be able to load the data into the array from there.

Just write your code straightly, it would work.

Quote:
> int main(int argc, char *argv[]){

> unsigned long address;
> unsigned long wordcount;
> unsigned long memory[MEMORY_SIZE], *p;

> p = &memory[0];

> .
> .
> .

> loader(*p);

Huh! How is loader() prototyped?

Quote:
> }

> void loader(unsigned long *p){
> //Load data into array

> ???

> }

> I am not sure if I've setup the pointer correctly.  Also once in the
> function how do I access the different array elements???

Be simple :

void loader(unsigned long *p)
{
 /* Load data into array */

Quote:
}

int main(void)
{
   unsigned long memory[MEMORY_SIZE];
   loader(memory);
   return 0;

Quote:
}

It should be a good idea to provide the number of elements to the function:

void loader(unsigned long *p, size_t len)
/* size_t wants at last <stddef.h> */
{
 /* Load data into array */

Quote:
}

int main(void)
{
   unsigned long memory[MEMORY_SIZE];
   loader(memory, sizeof memory / sizeof *memory);
   return 0;

Quote:
}

--
-hs- emdel at noos.fr "support Afghans against Talebans"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge


Wed, 10 Mar 2004 16:49:11 GMT  
 Newbie question: Pointer to an array and passing it into a function

Quote:

> Hi

> I have a large array of unsigned long and I want to populate the array
> within a function.  Rather than passing the entire array into the
> function, I want to just pass a pointer to the array into the
> function, and then be able to load the data into the array from there.

> int main(int argc, char *argv[]){

> unsigned long address;
> unsigned long wordcount;
> unsigned long memory[MEMORY_SIZE], *p;

> p = &memory[0];

> .
> .
> .

> loader(*p);
> }

> void loader(unsigned long *p){
> //Load data into array

> ???

> }

> I am not sure if I've setup the pointer correctly.

Not quite. Instead of loader(*p), it's just loader(p), or indeed
loader(memory) - yes, you did more work than you needed to! You see,
when you try to pass an array to a function, the compiler says "yeh,
right" and converts it to a pointer to the first element of that array.
To actually pass the whole array by value (not a good plan), you'd have
to do a bit of extra work which I won't go into now.

Quote:
> Also once in the
> function how do I access the different array elements???

p[i] = 42;

where i is the element you want (in your case, i must have a value
between 0 and (MEMORY_SIZE - 1).

Note: it would be a good idea to send loader() the size of the array
too, as this reduces the coupling between main() and loader(). Also,
give loader() a prototype.

Something like this:

void loader(unsigned long *p, size_t count);

int main(int argc, char *argv[])
{
  unsigned long address;
  unsigned long wordcount;
  unsigned long memory[MEMORY_SIZE] = {0}; /* initialises first element
to 0, and everything else to the default (which, for unsigned long, is
0). */

.
.
.

  loader(memory, MEMORY_SIZE);

/* or even this: */
  loader(memory, sizeof memory / sizeof memory[0]);

  /* main returns int, so let's return an int */

  return 0;

Quote:
}

void loader(unsigned long *p, size_t count)
{
  /* Load data into array */

  Valid values are p[0] through p[count - 1].

Quote:
}

HTH. HAND.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Wed, 10 Mar 2004 15:54:19 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. how do I pass arrays as parameters to functions - newbie

2. passing pointer to a function expecting an array

3. pass a pointer to array of structs to functions

4. passing array of pointers to function

5. Passing 2-D pointer to array to function

6. Passing an array of pointers to functions

7. Simple, Pass Pointer of an Array to function

8. function pointers and function pointers array

9. newbie question: passing struct as argument to function ?

10. newbie question: stings, pointers and arrays

11. newbie question about array of pointers

12. NEWBIE in array pointer question

 

 
Powered by phpBB® Forum Software