Problem with variable passed to function

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have the following code,

char MyArray1[2];
char MyArray2[2];
char MyArray3[2];
void MyFunction(char SerialInString[]){  // receives message from serial in function
    CharPos = 0;
            
    while(MyString[CharPos] != 61){ memcpy(MyArray1,(char*)SerialInString[1],1); CharPos++; }
    CharPos++;
    while(MyString[CharPos] != 61){ strcpy(MyArray2,(char*)SerialInString[CharPos]); CharPos++; }
    CharPos++;
    while(MyString[CharPos] != 61){ strcpy(MyArray3,(char*)SerialInString[CharPos]); CharPos++; }

....

I have tried over 20 different ways, and always get some sort of issue, closest i got was to get initially some chars into MyArray1, but then it got deleted when MyArray2 got filled, and MyArray3 got the chars it was suppose to, but got joined onto the back of it the chars from MyArray2.

weird!

thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

We can not say anything with the little facts (well ,hardly facts even..) you've given. 

 

Do this:

 

Write a minimal but complete program that builds but does not do what you want it to do.

 

Post the code here.

Describe what you want it to do.

Describe what you are observing it doing.

 

Right now it's like you phoned your car mechanic and said "I turn the ignition key, but my car won't start. Why?".

 

Give us complete facts, requirements and actual behavior and we can make an honest effort to help you.

 


Theone6000 wrote:
closest i got was to get initially some chars into MyArray1, but then it got deleted when MyArray2 got filled, and MyArray3 got the chars it was suppose to, but got joined onto the back of it the chars from MyArray2

This sounds like you're overflowing your arrays, trying to put more chars into them than they each can hold. In your case each array can hold 2 chars - and if you want to treat them as strings then you need a zero terminator in each of them so now they can only hold one meaningful char each. How many characters are you actually expecting for each of the arrays?

 

Theone6000 wrote:
I have tried over 20 different ways, and always get some sort of issue

Your frustration here is because youre practising "hipshot programming", i.e. trying code without actually having much clue as to what the effects will be. Could also be called "a shot in the dark". If you tell us what you actually want your function to do - on a higher level than bare code - then we might be able to give some good advice. Also, you need to get a good book on the C programming language and read it.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
it's like you phoned your car mechanic and said "I turn the ignition key, but my car won't start. Why?".

 

Or just like your previous post, where the problem turned out to be due to basic programming errors within a function - but you never showed the code inside that function!

 

How do you expect anyone to be able to remotely debug code that they cannot see?!?!

 

Your frustration here is because you're practising "hipshot programming"

Indeed.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

hipshot programming!  lol

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

AKA Shotgun Programming

AKA Throw The Dice Programming

AKA Crapshoot Programming

 

+ many more euphemisms!

 

Sadly, this is not just newbie territory. Mea Culpa. It seems often associated with not knowing what you are doing. Again Mea Culpa. Especially prevalent among those of us who are "self taught". Mea Culpa. I hope that my practice is now less subject to this style of programming, with huge thanks to those who have taken the time and effort so that a few of us from the unwashed masses can learn some important programming stuff.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sun. May 7, 2017 - 10:21 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"shot gun programing", more like 5000 yard snipering!, No one has ever put a cheque on the table for my code, and, requesting releasing of code without directly stating to convoluted it to avoid any future possible hacking is irresponsible!

 

serial in code########################################

char workingString[20];

uint8_t MyCount = 0;

ISR(USART_RX_vect){

    while(!(UCSR0A & (1 << RXC0))){}

    workingString[MyCount] = UDR0;

    if(workingString[MyCount] == 13){  // enter char

        workingString[MyCount] = '\0';

        UseString(workingString); 

        MyCount= 0;

        }

    else{ MyCount++; }

    }



file that does work##########################


char MyArray1[2];

char MyArray2[2];

char MyArray3[2];

uint8_t outStringPos = 0;

uint8_t inStringPos = 0;

void UseString(char MyNewString[]){

    while(MyNewString[inStringPos] < 48){ inStringPos++; } 

    while(30 < MyNewString[inStringPos] && MyNewString[inStringPos] != 61){ MyArray1[outStringPos] = MyNewString[inStringPos]; inStringPos++; outStringPos++; MySerialOut("Got here1 ",&MyNewString[inStringPos]); }

    inStringPos++;

    outStringPos = 0;

    while(MyNewString[inStringPos] != 61){ MyArray2[outStringPos] = MyNewString[inStringPos]; inStringPos++; outStringPos++; MySerialOut("Got here2 ",&MyNewString[inStringPos]); }

    inStringPos++;

    outStringPos = 0;

    while(MyNewString[inStringPos] != 61 && MyNewString[inStringPos] != '\0'){ MyArray3[outStringPos] = MyNewString[inStringPos]; inStringPos++; outStringPos++; MySerialOut("Got here3 ",&MyNewString[inStringPos]); }

    outStringPos = 0;

MySerialOut("1st char",MyArray1);

MySerialOut("2nd char",MyArray2);

MySerialOut("3rd char",MyArray3);

}



serial out file comand code ###########################


char MyCharString[20];

void OutMyMessage(){

    strcat(MyCharString,"\n\0");

    for(uint8_t t = 0; t < strlen(MyCharString); t++){

        while((UCSR0A & (1<<UDRE0)) == 0){ }

        UDR0 = MyCharString[t];

        }

    if(UCSR0A & (1<<UDRE0)){ UDR0 = 0; }

        _delay_ms(400);

    }

void MySerialOut(const char InOne[],const char * InTwo){ strcpy(MyCharString,InOne); strcat(MyCharString,InTwo); OutMyMessage(); }   




 

and the output of the program is when inputted aa=bb=cc #####################

 

Got here1 s=cv=er

Got here1 =cv=er

Got here2 v=er

Got here2 =er

Got here3 r

Got here3 

1st char as

2nd char cvas

3rd char ercvas

 

 

Added CODE formatting - JGM

Last Edited: Mon. May 8, 2017 - 10:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No Theone.

 

What you need to do is post here a minimal but complete program that we can copy into our AVR Studio and test ourselves. Everything needs to be there so that we  can build it. E.g. the necessary #include directives, function heads etc. The above is definitively not a program that builds clean. It does not even have a header for the main() function - something every C program must have or else it will not build (let alone run).

 

I would also advice you to take on one of the common coding styles. There is virtually no-one that load a whole bunch of statements on one line like you do:

while(30 < MyNewString[inStringPos] && MyNewString[inStringPos] != 61){ MyArray1[outStringPos] = MyNewString[inStringPos]; inStringPos++; outStringPos++; MySerialOut("Got here1 ",&MyNewString[inStringPos]); }

Get used to instead place only one statment on each line, e.g. like so:

while(30 < MyNewString[inStringPos] && MyNewString[inStringPos] != 61)
{
    MyArray1[outStringPos] = MyNewString[inStringPos];
    inStringPos++;
    outStringPos++;
    MySerialOut("Got here1 ",&MyNewString[inStringPos]);
}

This makes for much easier reading.

 

Even if you don't like that style you must understand that close to 100% of the people you are asking for help does. If you don't make an effort to spare the potential helpers a lot of effort to be able to actually read your code then they will probably not spend the effort to help you.

 

I started trying to clean up your code above in this respect, but gave up after a while. It is not us that should do that cleaning up - it is you.

 

After cleaning your minimal demonstration program up the simple way to post it here is to

1) Mark everything in the editor/IDE you use to edit your code.

2) Copy that.

3) Create a post here.

4) Select the code "tool" (the button marked with "<>".

5) Paste what you copied earlier.

6) Click OK.

7) Click the Post button.

 

You can see above in this post of mine what the code "tool" does in a post. A box with light blue background, and a monospaced font used for the code text. That makes it much easier to read.

 

Doing what I have advised you to here will increase you chances of us actually reading your code and try to help you sort it out. Not doing it will likely make people turn to other questions that they find more interesting and workable. It's totally up to you.

 

EDIT: You have shown us the actual otput of the program. Please tell us what output you expected/desired. This is important!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Mon. May 8, 2017 - 08:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
4) Select the code "tool" (the button marked with "<>".

Fully illustrated instructions here: https://www.avrfreaks.net/forum/s...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I can spot one problem already in the small part of code I cleaned up above. The code is supposed to collect two characters, e.g. "aa" in the example input data you have give. When it has done that it will call the MySerialOut() like so:

 

		    MySerialOut("1st char",MyArray1);

 

In MySerialOut you use the two parameters when calling strcpy and strcat. Both of those functions expect C strings as their parameters. C strings are not exactly the same as a simple array of characters. Compared to "plain" arrays there is one extra feature of C strings: They must have a null character as their last character - it marks the end of the string.

 

Now, the way you collect characters into e.g. MyArray1 it is your responsibility to add this null character. It is also your responsibility to make sure the array can hold all the characters you collect and the terminating null character.

 

So, you need to change the declaration to

char MyArray1[3];  // Two characters and a null terminator. 2 + 1 -> 3

and then make sure you have the terminating null character ending the string:

 

    while(30 < MyNewString[inStringPos] && MyNewString[inStringPos] != 61){
		MyArray1[outStringPos] = MyNewString[inStringPos];
		inStringPos++;
		outStringPos++;
		MySerialOut("Got here1 ",&MyNewString[inStringPos]);
	}
    MyArray1[outStringPos] = 0;      // <-- Null terminator!

 

Caveat Emptor! I make no guarantees that the above is correct to the last letter. I also make no guarantees that this is the only problem with your code.

 

Had you posted a complete program that builds clean I could have tested it in AVR Studio, but I was not in the mood to convert what you posted into something I could actually run. (Again that is what you should have done.)

 

HTH!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Mon. May 8, 2017 - 08:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JohanEkdahl wrote:
There is virtually no-one that load a whole bunch of statements on one line like you do

To be fair, that is probably just a result of failing to use the 'Add Code' button - so the forum just wraps it as if it were prose ...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
To be fair, that is probably just a result of failing to use the 'Add Code' button - so the forum just wraps it as if it were prose ...

Perhaps.  Also, perhaps someone once told OP that single-line functions are appropriate. devil

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
perhaps someone once told OP that single-line functions are appropriate.

Some people do seem to believe that "packing" the 'C' source actually "optimises" the code ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What!!!! it doesn't???? It used to work with my basic programs on the trs80.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I park code for the primary reason of neatness and shortening the amount of code lines, nothing is more infuriating than being at one end of a 200 line file, then have to go to the other end, when you find the line, forgotten what your suppose to do!.

I park loops usually because they are short and small, and easy to understand, and i neaten them as such:-

for(int t = 0; t < 10, t++){           a = b;            b += c;               }

 

regardless, I fixed the code! the problem was the end char, and the size of the array, I also had the while loops to separate the arrays for bug fixing, all that is actually in a loop!

thanks

 

edited, but only "flattened" loops when there only a few calls, and, short calls, anything that goes wider than a page and/or is complex is to be spread across the appropriate amount of lines!

 

Last Edited: Tue. May 9, 2017 - 02:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok. You're your own boss here.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Theone6000 wrote:
I park (sic?) code

"pack" ?

 

for the primary reason of neatness

Fair enough - beauty (including "neatness") is, as they say, in the eye of the beholder.

 

Just be aware that most people here will, as Johan, find that it makes the code harder to read.

 

 nothing is more infuriating than being at one end of a 200 line file

That's why we break things up into functions.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. May 9, 2017 - 06:20 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Code is hard to read on two grounds, it is completely non-uniform, or one is use to old, pre-modern coding standards, and do not want to accept/conform to modern standards, just like an old person that does not like computers, at the end of the day it is not because they can't use computers, it is just that they are not use to the idea, nor do they want to accept the idea!

 

Edit:-

"Beautifying" loops also extends to "beautifying functions, why have "void MyFunction(){ a = b + c; } spread over 4 lines, when it can be neatly done in one?, and we all have had functions that are large (and small), in the end, complex functions can take up tens of lines, so compress the small functions, the loops, and get a 200+ file down to 50-80, and "take it all in in one gracious look!"

 

Last Edited: Tue. May 9, 2017 - 07:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you write code that works, then nobody needs to look at it!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

good point!, we all wish we could write code that works, but at some stage we are all newbies at a language that we are new too, and need the guidance of the gurus of that language!, but most programers will know, it is fun to meet and groups and conventions and "talk shop" if we all were 100% good at something instantly, it would take the "fun" out of it wouldn't it!?