passing arrays

I have a hopefully super simple question.
How do you pass an array of characters to a function?

my pseudo code is:

void CollectData()
{
char buffer[1056];

...fill the buffer...

StoreData(buffer);
}

void StoreData(char data[])
{
   if(sizeof(data) != 1056)
  {
    printf("The buffer is not full. Used %d bytes.", sizeof(data));
  }
  else
  {
   ... store the data ...
  }
}

In my program the buffer is full, but the sizeof(data) always returns 2 bytes. Why is that?

This seems almost trivial, but I can’t get it. I don’t want to pass by reference because the buffer could be updated before the data gets stored.
If I assume the array was passed successfully, it appears all the data in the function is the same as the buffer passing to it.

Am I missing something?

Thanks,
myriad

[QUOTE=myriad;1166]I have a hopefully super simple question.
How do you pass an array of characters to a function?

my pseudo code is:

void CollectData()
{
char buffer[1056];

...fill the buffer...

StoreData(buffer);
}

void StoreData(char data[])
{
   if(sizeof(data) != 1056)
  {
    printf("The buffer is not full. Used %d bytes.", sizeof(data));
  }
  else
  {
   ... store the data ...
  }
}

In my program the buffer is full, but the sizeof(data) always returns 2 bytes. Why is that?

This seems almost trivial, but I can’t get it. I don’t want to pass by reference because the buffer could be updated before the data gets stored.
If I assume the array was passed successfully, it appears all the data in the function is the same as the buffer passing to it.

Am I missing something?
[/quote]

First of all you really shouldn’t declare a buffer that size to be auto - declare them globally, statically, or dynamically allocate them (don’t forget to de-allocate!). Second, you really don’t want to push that much data on the stack (which is where arguments go when calling functions)!

sizeof is telling you the size of the pointer called ‘data’ even though you added []'s. sizeof is not a magic counter - it will tell you sizeof(buffer) is 1056 bytes whether you’ve put anything in it or not.

If you MUST maintain buffer integrity you may want to rethink your producer and consumer task relationships and incorporate some semaphoring logic.

In general it looks like you need to add an index pointer and flag variable to your code for starters. Good luck!

Interesting. I hadn’t considered the stack yet.

After digging through general C sites, it appears that when passing the buffer the way I did, C automagically converts it to a pointer and looses all the details . Since the size of the buffer is always the same I don’t need to pass in extra info. At least, it seems to be working this way for now. Of course I need to test it extensively.

Many years ago when studying C in school, the professor said to never create global variables. But now I’m working with micros so it’s a different story. Thanks for the tip!