Thread: arrays and structs

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    arrays and structs

    Hey guys I am having trouble with the following code:

    Code:
    #define BYTE unsigned char
    
    struct sentence
    {
    
    BYTE warningFlag[1];
    BYTE cycleLockFlag[1];
    BYTE xte[10];
    BYTE dirToSteer[1];
    BYTE xtDevUnits[10];
    BYTE cirArrAlarm[10];
    BYTE perpArrAlarm[10];
    BYTE orgToWPt1[10];
    BYTE orgType[1];
    BYTE nxtWPtId[10];
    BYTE hts[10];
    BYTE htsType[1];
    BYTE checkSum[1];
    };
    
    struct sentence apb;
    
    
    char testMethod()
    {
    
    BYTE tempxte[10];
    BYTE ch;
    int i=0;
    
    	while((ch=WaitRx())!=',') //WaitRx() 
    //WaitRx() reads in a char at a time
    	{
    		tempxte[i]=ch;
    		i++;
    	}
    
    return *tempxte;
    }
    
    void parseAPB(){
    
    skipComma(1); //sentence delimeted by commas
    //skipcomma skips 1 comma in sentence
    
    *apb.warningFlag = testMethod();
    printline(apb.warningFlag);
    printline(",");
    }
    The struct contains information about a sentence.
    testMethod() reads the data into tempxte until a comma is found and then returns this value.
    parseAPB() assigns the returned value from testMethod() into the struct, apb.warningFlag, this is then printed.

    The problem is that only the first character from the parsed line is output, i.e
    actual input "hello",
    what is copied to warningFlag = 'h'

    I think I have messed up my pointers or something crazy, can anyone help?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yep, you sure have. You only have 'warningFlag' containing one BYTE.
    Code:
    struct sentence
    {
    
    BYTE warningFlag[1];
    BYTE cycleLockFlag[1];
    BYTE xte[10];
    BYTE dirToSteer[1];
    BYTE xtDevUnits[10];
    BYTE cirArrAlarm[10];
    BYTE perpArrAlarm[10];
    BYTE orgToWPt1[10];
    BYTE orgType[1];
    BYTE nxtWPtId[10];
    BYTE hts[10];
    BYTE htsType[1];
    BYTE checkSum[1];
    };
    Even before I read anything about your problem itself, I was going to post and ask you why on earth you have arrays that only hold one thing. Turns out, that's what you were posting about.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Sorry, I was a bit unclear, the data in the struct with only one byte being held only have to store one byte. warning flag can only ever store a single character. However, xte can store up to 10 bytes. I am having trouble getting the data parsed into the apb.xte. Whenever I print out xte only 1 byte is displayed and I need all the data to be displayed.

    Thanks for your help

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well if it's going to be only one byte, dont make it an array. There's no point.
    Code:
    struct sentence
    {
        BYTE warningFlag;
        BYTE cycleLockFlag;
        BYTE xte[10];
        BYTE dirToSteer;
        BYTE xtDevUnits[10];
        BYTE cirArrAlarm[10];
        BYTE perpArrAlarm[10];
        BYTE orgToWPt1[10];
        BYTE orgType;
        BYTE nxtWPtId[10];
        BYTE hts[10];
        BYTE htsType;
        BYTE checkSum;
    };
    As to your problems with xte, how are you filling it up? If you're trying to display it with string functions, then that gives you 9 bytes of data and 1 byte for the null character. Assuming it was filled with something, say the word "hello", you could display it something like so:
    Code:
    struct sentence instance;
    
    ...fill up 'instance'...
    
    printf("%s\n", instance.xte );
    However, you are going to have to be filling each member of your structure individually.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    I am using the testMethod() function. This function reads a byte at a time until it reaches a comma, once it reaches a comma the tempxte variable is returned i.e.

    apb.xte=testMethod(); this reads the data in then returns it to the apb.xte.


    the actual value to be stored in apb.xte is 0.01, and when the apb.xte is printed out only 0 is dispayed, I am sure the testMethod used for parsing the line works, i think it is something to do with my pointers, I dont think i am storing the returned variable correctly

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, no, it doesn't. Try turning on your compiler's warnings. You cannot make assignments to entire arrays with the assignment operator. You must fill each element in one at a time.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    look here

    Code:
    char testMethod()
    {
    
    BYTE tempxte[10];
    BYTE ch;
    int i=0;
    
    	while((ch=WaitRx())!=',') //WaitRx() 
    //WaitRx() reads in a char at a time
    	{
    		tempxte[i]=ch;
    		i++;
    	}
    
    return *tempxte;
    }
    
    void parseAPB(){
    
    skipComma(1); //sentence delimeted by commas
    //skipcomma skips 1 comma in sentence
    
    *apb.warningFlag = testMethod();
    printline(apb.warningFlag);
    printline(",");
    }
    you are returning a pointer from the testMethod function.

    ***but look the return type of the function testMethod its is char instead of char***3

    Code:
    char * testMethod()
    {
    
    BYTE tempxte[10];
    BYTE ch;
    int i=0;
    
    	while((ch=WaitRx())!=',') //WaitRx() 
    //WaitRx() reads in a char at a time
    	{
    		tempxte[i]=ch;
    		i++;
    	}
    
    return *tempxte;
    }
    
    void parseAPB(){
    
    skipComma(1); //sentence delimeted by commas
    //skipcomma skips 1 comma in sentence
    
    *apb.warningFlag = testMethod();
    printline(apb.warningFlag);
    printline(",");
    }

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish
    look here

    Code:
    char testMethod()
    {
    
    BYTE tempxte[10];
    BYTE ch;
    int i=0;
    
    	while((ch=WaitRx())!=',') //WaitRx() 
    //WaitRx() reads in a char at a time
    	{
    		tempxte[i]=ch;
    		i++;
    	}
    
    return *tempxte;
    }
    you are returning a pointer from the testMethod function.

    ***but look the return type of the function testMethod its is char instead of char***3
    No they aren't. See the dereferencing operator? They're dereferencing the pointer, which gives you the value of a single character, which matches the function prototype. (Of returning one single character.)

    However, that's not the real problem. The problem is as already stated. You cannot assign entire arrays in one shot. You have to do it individually.

    Now let's look at the problems in your version:
    Code:
    char * testMethod()
    {
    
    BYTE tempxte[10];
    BYTE ch;
    int i=0;
    
    	while((ch=WaitRx())!=',') //WaitRx() 
    //WaitRx() reads in a char at a time
    	{
    		tempxte[i]=ch;
    		i++;
    	}
    
    return *tempxte;
    }
    tempxte is an array, which is not static. It therefore cannot be returned, or rather, you cannot return a pointer to it. Well, that's not entirely true, you can return a pointer. C will allow it, however, what it points to is gone, vanished off of the stack, when the function call ends. So in returning a pointer to it, you're returning an invalid pointer, which cannot be safely used.

    Furthermore, your version still dereferences the array name, and still only gives you one character returned, which does not in fact match the function definition.

    Their version "works" as far as C is concerned, just not the way they want it to. As stated, they'll have to rethink this. (Something like passing the array to filll as an argument could work.)

    Quzah.
    Last edited by quzah; 02-25-2005 at 01:33 AM.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Would it make much of a difference if I declared BYTE tempxte[10] as a global variable?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, it would make nine BYTEs difference. What you really need to do is think about what you are in fact trying to get done. Do you need one byte? Do you need 10 bytes? How about 100? How much data do you actually need? Is this the best way to go about whatever it is you're trying to do.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    quzah, you are right. but when looking at the code he is returing a single charcter which is not actually needed. if he could send the base address of the array somehting like &tempxte[0]. hope he could solve the prob. and more over look at this
    Code:
    struct sentence apb;
    
    return *tempxte;
    
    *apb.warningFlag = testMethod();


    what do you think, about this??

    s.s.harish

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ssharish
    quzah, you are right. but when looking at the code he is returing a single charcter which is not actually needed. if he could send the base address of the array somehting like &tempxte[0]. hope he could solve the prob. and more over look at this
    Code:
    struct sentence apb;
    
    return *tempxte;
    
    *apb.warningFlag = testMethod();
    what do you think, about this??

    s.s.harish
    I think that code is even worse. Here's why:
    Code:
    return *tempxte;
    This has already been covered. But we'll recap:
    1) 'tempxte' is a local variable, which as soon as the function ends, is gone. It doesn't matter if you were returning the address, it still is gone, out of scope, off the stack, poof, gone.
    2) * does not give you the address of anything. If used in combination with a pointer, it gives you the value at the pointer, not the address of a variable. You're thinking of the address-of operator, which is: &
    3) Even if * were the address-of operator, you still wouldn't be returning the right thing.

    Code:
    struct sentence apb;
    ...
    *apb.warningFlag = testMethod();
    1) apb is an instance of a structure. Not a pointer to one.
    2) warningFlag is not a pointer either.
    3) Even if one or both of these were pointers, the syntax you're trying to use is wrong.
    4) Since warningFlag is a single byte, all you're assigning to it, at best, is a single character.
    5) Even if warningFlag were an array, the assignment would be incorrect.
    6) Even if warningFlag were a pointer, the assignment still would be incorrect, because as mentioned, the data inside the function is GONE as soon as the function ends, because it's a non-static, local variable.

    What I think is that you need to get a really good C book, and start from the very beginning, going slowly, to try and learn the basics of the language. Almost every single piece of code you post is wrong. It's well and good that you like to help, but you need to make sure you're accurate, at least somewhat, before trying.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. allocatable arrays in CLASSes and STRUCTS
    By simone.marras in forum C++ Programming
    Replies: 4
    Last Post: 03-14-2009, 10:50 AM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  3. arrays in structs
    By *DEAD* in forum C Programming
    Replies: 3
    Last Post: 11-25-2006, 07:35 AM
  4. malloc for structs and arrays
    By rkooij in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 07:38 AM
  5. dynamic arrays of structs...
    By matheo917 in forum C++ Programming
    Replies: 8
    Last Post: 12-14-2002, 06:57 AM