This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

What will happen if I pass an element of a 'char' array to a function expecting an int

I know this is probably a basic question about storing of variables in memory but...

First, if I take a 16bit integer, split it into 2 bytes, then place these into adjacent elements in a char array.

Then, if I call a function expecting to be passed a 16bit integer and give it an element of the char array.

Will it copy the contents of the array element specified and then copy the next one automatically too?

Note: I might have reversed MSB and LSB below?

Is this bad programming?


/* Variables/Functions Used */

char testFailRef [16];
unsigned int testIndex;
unsigned int testCycleCount;

LCD_senduwasciidec4bit(unsigned int data);

//This function takes a 16 bit integer as input, then outputs this number on the LCD display (i.e. as seperate 'chars')
//It will only output the correct number of digits based on the size of the variable
//i.e. If data = 1500, it will send "1""5""0""0" to the LCD as 4 chars.

/* Write to array */
resultBuffer.testFailRef[0] = testIndex;
resultBuffer.testFailRef[1] = (ubyte)(testCycleCount & 0xff);        //LSB
resultBuffer.testFailRef[2] = (ubyte)((testCycleCount >> 8)& 0xff);  //MSB

/*Read from array */


//Option #1
LCD_senduwasciidec4bit(((uword)resultBuffer.testFailRef[1]) + ((uword)resultBuffer.testFailRef[2]<<8));

//Option #2
LCD_senduwasciidec4bit(resultBuffer.testFailRef[1]);

Parents
  • Then, if I call a function expecting to be passed a 16bit integer and give it an element of the char array.

    Will it copy the contents of the array element specified and then copy the next one automatically too?

    Of course not. C does what's called "pass by value", i.e. the called function gets the value of what you passed it. In the case at hand, that's a number between 0 and 255 (or -128 and 127, depending on a compiler switch).

    Is this bad programming?

    Not yet. But the way you're asking the question indicates you're setting off in the wrong direction.

    In the case you present, there's no need whatsoever to rip your 16-bit unsigned into individual bytes, thus no need to worry what will happen to it once it's split up. You have a uint16_t object, and a function that wants exactly such a thing for its argument. Why convert/split/join anything?

Reply
  • Then, if I call a function expecting to be passed a 16bit integer and give it an element of the char array.

    Will it copy the contents of the array element specified and then copy the next one automatically too?

    Of course not. C does what's called "pass by value", i.e. the called function gets the value of what you passed it. In the case at hand, that's a number between 0 and 255 (or -128 and 127, depending on a compiler switch).

    Is this bad programming?

    Not yet. But the way you're asking the question indicates you're setting off in the wrong direction.

    In the case you present, there's no need whatsoever to rip your 16-bit unsigned into individual bytes, thus no need to worry what will happen to it once it's split up. You have a uint16_t object, and a function that wants exactly such a thing for its argument. Why convert/split/join anything?

Children
No data