none
Problem in casting character array into structure

    Question

  • I am trying to cast the pointer of a character array to structure which is having only character elements in it. Size of array is same as that of structure. When I decompress some data and store it in array and try to cast this array pointer to structure I get all the characters got stored in first element of structures even if the size of first char array in structure is defined. Following is snippet of my code:

    #pragma pack(push,1)
    struct cn
    {
     char symbol[10];
     char series[2];
     char maketType[1];
     char bestBOP[10];
     char bestBOQ[12];
     char bestSOP[10];
     char bestSOQ[12];
     char ltp[10];
     char tradedQty[12];
     char securityStatus[1];
     char openPrice[10];
     char highPrice[10];
     char lowPrice[10];
     char onlineIndex[8];
    }cn_t;


    lets say I have data in an array Info[sizeof(cn_t)]

    when I cast

    cn *cnPtr = (cn*) &Info;

    and then try to get individual fields i get all the data of Info aray till end in each of the elements
    but if I try to make them null terminated as below

    cnPtr->symbol[10] = '\0'

    then I couldnt get anything in any of the rest of the member elements, they appear as blank.


    please help me in casting this array pointer and retrieving strucuture elements.

    Thanks in advance, but it too much urgent so please help
    Friday, December 19, 2008 7:14 AM

Answers

  • Other way to read the data shown below:

    struct cn InfoCN;

    memcpy(&InfoCN, output, sizeof(cn_t));

    char *p = InfoCN.symbol;

    p[9] = '\0';

    Similarly access other members of the structure.

    I assume 'output' pointer contains the packet received from the socket.

    • Marked as answer by Wagas Friday, December 19, 2008 12:05 PM
    Friday, December 19, 2008 11:16 AM
  • Since you are not getting the null terminated string from the pakcet that you receive from the socket,
    this is how you can extract the structure members and append null. Otherwise, you can directly access the structure members after memcpy.
    • Edited by Mahendran Arumugam Friday, December 19, 2008 12:36 PM Edit
    • Marked as answer by Wagas Friday, January 02, 2009 8:05 AM
    Friday, December 19, 2008 12:35 PM

All replies

  •  cnPtr->symbol[10] = '\0' should be cnPtr->symbol[9] = '\0'.

    Structure elements can be accessed with structure variables. Why array?
    Friday, December 19, 2008 7:27 AM
  • Thanks, I want to access cnPtr->symbol, cnPtr->series etc..
    but when I try to print cnPtr->symbol, I get whole of the charcter array Info printed instead of 10 characters only similary when I try cnPtr->series I get the first two characters correctly but rest of array is still appeded to these two characters, same goes on for rest of the members, please tell me how can I access individual elements correctly reading upto their length only.
    Actually they are becoming not null terminated and thats why when printing them they are extending their length but need to print this structure in pipe separated format in a file, please show me an efficient way as speed is an issue, I also did try by copying individual elements in some temp array them appending a null character but thats making the code slow.
    Please help I need to maintain speed also
    Friday, December 19, 2008 7:36 AM
  • Just by using the structure variable, you can access the all the structure members. I don't understand why you are taking the values into array.

    If you can post the code, how you assign the values and retrieve the values from the structure i will try to correct it.
    Friday, December 19, 2008 8:56 AM
  • Code is very much huge, so i am trying to explain steps and mentioning some relvant code for it

    I am reading from a socket and recieving some compressed data, this compressed data can have different types of packets in it, so I read this compressed data into an array and then uncompressing it to get different packets inside it and depending on its packet type I am populating it in structure where I am facing the above mentioned problem. Following is some code from where I am taking one of the packets out from the uncompressed data array

    if(strncmp(hd->code,"CN", 2) == 0)
         {
          rec++;
          //starting from the left to read index of output buffer
          l = 0;
          for(j = m; j < m + dataLen - 1; j++)
          {
           InfoCN[l] = output[j];
           l++;
          }
          InfoCN[l] = '\0';
          
          //hThread = CreateThread(NULL, 0, writeTextFile, &Info, 0, &dummy);
          writeTextFile(1, &InfoCN);
          
         }

    In above InfoCN is the array finally I got to populate my structure mentioned above and then I want to access structure members and write them in pipe seperated format into a file
    writefile is doing something as below

    writeTextFile(int type, void *ptr)
    {
    char str[256];
    if (type == 1){
    cn *cnPtr;
    cnPtr = (cn*)ptr;
        sprintf(str, "CN|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s##",trim(cnPtr->symbol),trim(cnPtr->series),trim(cnPtr->maketType),trim(cnPtr->bestBOP),trim(cnPtr->bestBOQ),trim(cnPtr->bestSOP),trim(cnPtr->bestSOQ),trim(cnPtr->ltp),trim(cnPtr->tradedQty),trim(cnPtr->securityStatus),trim(cnPtr->openPrice),trim(cnPtr->highPrice),trim(cnPtr->lowPrice),trim(cnPtr->onlineIndex));
    }
    //Writing str to file below here

    fprintf(fp, str, strlen(str));
        //fprintf(fp, "\n", 1);
        fclose(fp);
    }

    Please let me know if still it is not clear.
    Friday, December 19, 2008 9:22 AM
  • Will the size of the data packet that you copy to the arry same as your structure size?

    For example, char symbol[10]

    Will the length of the symbol's value from the packet be always equal to 10(including the null character)?

    If so, you can insert the null character properly after reading each value and put it into your structure.


    Friday, December 19, 2008 9:36 AM
  • yeah it is of same size
    sorry but 10 characters is something without null character, so do u advice me to increase length of each element in structure by one and populate structure instead of populating char array?? Will it be efficient if I use so many if condition during populating strucuture?? my other structures are having upto 25 members so do I have to use that much of if statements???

    Friday, December 19, 2008 9:49 AM
  • If the packet that you are getting from the sacket gives you the null terminated string, then obviously you have to accomodate the null character by increasing the length.
    Friday, December 19, 2008 10:15 AM
  • no data from socket is not having null character space i just know the sizes of data elements excluding null...is there any way to use string data type where I can define the size of string as if i define a string of 10 lenght (just example) then it will add a null character itself? so if there is way to define the length of string then please tell else it will be long code if i copy character by character and then adding null character as use of strncpy is slowing down the code which I have already tried.
    Friday, December 19, 2008 10:30 AM
  • Other way to read the data shown below:

    struct cn InfoCN;

    memcpy(&InfoCN, output, sizeof(cn_t));

    char *p = InfoCN.symbol;

    p[9] = '\0';

    Similarly access other members of the structure.

    I assume 'output' pointer contains the packet received from the socket.

    • Marked as answer by Wagas Friday, December 19, 2008 12:05 PM
    Friday, December 19, 2008 11:16 AM
  •   Thanks alot, this way if I want to write them in a file then i have to make as much char pointers as I am haiving elements?? And then concatinating them to make a single string and then writing to a file...is it the only best possible fastest way to do the task???
    Friday, December 19, 2008 12:05 PM
  • Thanks alot, this way if I want to write them in a file then i have to make as much char pointers as I am haiving elements?? And then concatinating them to make a single string and then writing to a file...is it the only best possible fastest way to do the task???
    Friday, December 19, 2008 12:05 PM
  • Since you are not getting the null terminated string from the pakcet that you receive from the socket,
    this is how you can extract the structure members and append null. Otherwise, you can directly access the structure members after memcpy.
    • Edited by Mahendran Arumugam Friday, December 19, 2008 12:36 PM Edit
    • Marked as answer by Wagas Friday, January 02, 2009 8:05 AM
    Friday, December 19, 2008 12:35 PM