Assigning the value in an array to variable in C

Discussion in 'Mac Programming' started by subsonix, Dec 14, 2008.

  1. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #1
    Hi,

    Trying to figure out how to get the content of an array and assign it to a variable. Im readig data from a file into an array and the data is being stored one byte per slot in the array. How can I get a value that is stored across several bytes into one variable?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
      char list[] = {(char)1,(char)2,(char)8,(char)12,(char)7};
      char single;
      single = list[2];
      printf("The value is: %d\n",(int)single);
      return 0;
    }
    
    The [] operator takes a pointer (to the base of an array) and a subscript that is multiplied times the size of the type that the first operator points to, which is added to the base address to get the address of the particular element referenced. You will use this operator to access elements of your arrays. You said you had a list of bytes, so I used char, as that's the only single byte type in C.

    -Lee
     
  3. subsonix thread starter macrumors 68040

    Joined:
    Feb 2, 2008
    #3
    But what happens if a value is more than one byte big? In my case I have ended up with a 4 byte value stored accross 4 index postions. The content of the array at the index I´m interested in is: {00, 00, 4d, f8}

    I would like to store the value 4df8 in one variable.
     
  4. BravoBug macrumors newbie

    Joined:
    Dec 8, 2008
    Location:
    Oregon, USA
    #4
    You basically just memcpy the bytes from one spot to the other. The below example assumes int will be 4 bytes, which is not always the case. It also assumes your bytes are already in the proper order you want. You have to make sure you're copying the bytes in the proper way. But as far as just moving the bytes around, memcpy should work just fine.

    Code:
    	unsigned char someBytes[4];
    	someBytes[0] = 0x00;
    	someBytes[1] = 0x00;
    	someBytes[2] = 0x4d;
    	someBytes[3] = 0xf8;
    	
    	int aVariable;
    	
    	memcpy(&aVariable, someBytes, 4);
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    Ooops. Didn't read the whole thing. You could try this:
    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
      char list[] = {(char)0,(char)0,(char)0x4d,(char)0xf8};
      int *iList = NULL;
      int single;
      iList = (int *)list;
      single = iList[0];
      printf("The value is: %d\n",single);
    }
    
    This becomes endian-dependent though. You have to be sure that you are running on an architecture with the same endianness as the file was written from, otherwise your bytes will be interpreted incorrectly and you will not get the values you expect. For reference, the PowerPC chips can run in either, but were used Big-endian in macs, and x86 processors are little-endian.

    -Lee

    Edit: BravoBug posted a workable solution as well, but it requires copying of the memory around, which is fine for one value, but if you have a huge array playing with the pointers as I demonstrated might be quicker/easier.

    P.S. Sorry for the assumption earlier, we get a whole range of experience levels.
     
  6. subsonix thread starter macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    Thank you guys for the solutions and heads up on issues with endianess. This worked perfectly. I was messing around with copying to a second array at first which started to get a bit messy, I was hoping for a C command like memcpy for this, excelent.

    I thought of dealing with potental endian conversion while copying the array, but now I probalby use CFSwapInt32 or something similar from core foundation if I need to change it.

    Cheers.
     
  7. litesgod macrumors newbie

    Joined:
    Apr 3, 2008
    #7
    Maybe outside of the scope of the original question, but I would get used to using htons/htonl. These are standard C functions (found in inet.h I think) that convert from host byte-order (little endian on x86) to network byte-order (big endian everywhere). As part of the standard C library, they will be on any machine, and on machines where host byte-order and network-byte order are the same, they just do nothing. Also useful are the reciprocals ntohs and ntohl.
     
  8. subsonix thread starter macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    Thanks for that, I will look into htons/htonl. Seems like a good advice, I did not know about these. I´m pretty new to C.
     

Share This Page