Thread: 256 array problem

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    19

    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. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    use
    Code:
    unsigned char ga[256];
    Kurt

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

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    19
    Thanks, works perfect now, wasnt familiar with the unsigned int variable.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    19
    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. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    19
    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. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    19
    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. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Dec 2005
    Posts
    19
    ok thank you, i think i get it now

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    19
    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. #13
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    >>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. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM