1. ## 256 array problem

Hi, i am trying to define a 256 unsigned character array, with [0]=0, [1]=1 etc rigth up to [256]=256.

This is what i am using as the function
Code:
int n=0;
char ga[256];

while (n!=256)
{
ga[n]=n;
n=n+1;
}
up to [127], which equals 127, all are corret, however [128] and everything after this is erroneous. I am guessing this is because of the int value of n, however how can i modify this correctly?

Thanks

2. use
Code:
unsigned char ga[256];
Kurt

BTW ga[256] = 256 is past the array bounds

3. Well actually, there is no "ga[256]". At best all you can get in your declaration is "ga[255]" due to the way arrays are numbered in C. Finally, it appears as though char is signed by default on your system. Thus, everything over 127 causes fun overflow of the varaible.

Quzah.

4. Thanks, works perfect now, wasnt familiar with the unsigned int variable.

5. ok, now i need to pass these values from this global array into a local array of 256 long integers. How do i do this?

6. You mean copy it over? Or use it as an argument to a function?
1 - Loop through each element in the array, assigning from one to the other.
2 - Use the name of the array as the argument to the function you're calling.

Quzah.

7. i mean copy it, but i need the long integer array to be defined in a separate function, how do i pass each value from the main program?

8. Well you said it's global, so you don't need to pass anything. But if you really want to...
Code:
void somefun( char a[] );

char somearray[ SOMESIZE ];
...

somefun( somearray );

Quzah.

9. Ok, i dont think you understand what i mean, this is waht i have been asked to do:

Write a c program that declares a glbal array of 256 unsgned characters. In the main program initialise the global array to have the values 0-256.
Declare a local array of 256 long integers and copy the values from the character array.

This is what i have so far:

Code:
main()
{
int n=0;
unsigned char global[256];

while (n!=256)
{
global[n]=n;
n=n+1;
}

return (0);
}

long int local

10. No, I understand. You just don't seem to understand what the term 'global' means. A 'global' variable is a variable that has been declared outside of all functions. This is an example of a global integer:
Code:
#include<stdio.h>

int x; /* x is global, as in, it's declared outside of any function */

int main( void )
{
x = 10; /* It is global, and available to functions in the same scope. */

printf("x is %d\n", x );

return 0;
}
Also, again, your array will not have the value 256 in it, because arrays start at 0 and range through size-1. Thus, if you have an array like so:
Code:
int array[ 10 ];
The only values you'll get, if you put the value of the index in the variable, is 0 through 9. Not 10.

Quzah.

11. ok thank you, i think i get it now

12. But how will i b able to fill the global array with the values up to 256 if it is outside any funtion, i wont b able to use a while loop

13. >>But how will i b able to fill the global array with the values up to 256 if it is outside any funtion, i wont b able to use a while loop
shure you can ( thats why it's called global ) just try it.
Kurt

14. Originally Posted by kris_perry2k
But how will i b able to fill the global array with the values up to 256 if it is outside any funtion, i wont b able to use a while loop
You can access global variables from any function so a while loop will work fine. If you want values from 0 to 256 then your array must contain 257 elements.