NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » use of strcat fuction

September 23, 2012
by sask55
sask55's Avatar

I am having an issue passing arguments to strcat function.

Can I use the strcat function to concatenate one string onto another on the MCU?

Can anyone advice me what these warnings are indicating when I compile may code?

Warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast

I get that warming message for each line of code where I have tried to use strcat when I compile the code. The code does load but is not working as I expected I don’t understand what the message is saying. I have very little experience using c string arrays or pointers.

copy of variable def stamtents

         //holder vaiable for date time
         char incoming = 0;
         char WkDay[10];
         char Date[19];
         char Time [12];
         uint8_t LC;

copy of the vatiable fill statments

      // read char and fill WkDay untill , is read
        strcpy (WkDay,"");
         while  (incoming != 12){
      incoming = uart_read();
      strcat (WkDay, incoming);
      }

    strcpy (Date,"");

    // fill Date for 5 char after , is read  
    while  (incoming != 012) {
      incoming = uart_read();
      strcat (Date, incoming);
      } 
    for (LC =1;LC <=5;LC++){
        incoming = uart_read();
        strcat (Date, incoming);
      }

      strcpy (Time,"");

    // fill Time untill end char is read
    while  (incoming != 76){
      incoming = uart_read();
      strcat (Time, incoming);
      }

Strangely the indented code sections are not showing up as code in the Preview. I have no idea why not.

Perhaps this is not even the correct approach to fill string variable one charter a time as they are returned form the uart_read function. Any comments or ideas would be appreciated.

September 23, 2012
by JimFrederickson
JimFrederickson's Avatar

At times the indented code has not show in up in Preview for me as well. (Sometimes even when viewing the page it has not too.)

In Preview I have sometimes been able to just 'refresh' and then it would show up.

Originally, when I had the problem, it was when I was using an previous version of Firefox.

Since the last three numbered releases, I have not had this issue. (I am on the current release of Firefox now. 15.x.x)

I don't know if the older versions of Firefox had anything to do with that, but it was quite annoying for awhile. (I don't post that much, so maybe I am just having a 'lucky streak' and it doesn't have anything to do with versioning...)

September 23, 2012
by Noter
Noter's Avatar

I think you would benefit from studying a bit more about strings and string pointers. There are lots of tutorials on the web and here is one that looks pretty good. http://www.taranets.net/cgi/ts/1.37/ts.ws.pl?w=329;b=280.

As you know a string is a character array with a null value marking the end of the string. So to add a single character to the end of the array you can either search for the null or keep track of it along the way so you know where to insert the new character. By the time you want to use any of the string functions, the array must be null terminated and you pass the address of the array (which is a pointer) to the function. So you can't really use string functions to insert or concatenate a single character because a string with only a single character still has two characters since the null terminator must be part of it.

If you track the next position you can do something like this:

int i_next;  
char WkDay[11];  // add an extra byte for the terminator

then ...

i_next = 0;
memset(WkDay,0x00,sizeof(WkDay));
while  ((incoming = uart_read()) != 012) {
  WkDay[i_next++] = incoming;
}

But if you enter more than 10 characters the memory following your WkDay variable will be overwritten and often it is code which crashes your program. In a practical application you would also ensure maximum lengths were not exceeded.

September 23, 2012
by sask55
sask55's Avatar

Thanks Noter, I am going to go thru the link you posted I did realize that I would require an extra character in each array for the terminator character. Half the time my mind is somewhere between C coding and VBA coding so I end up with something that is not really anything at all. I have done much more VBA coding over a much longer period of time. Although ByVal and ByRef in VBA are somewhat equivalent in concept to pointers in C, I never really found it necessary to make use of them in VBA. The default ByRef always seemed to work well for what I was doing

I was kind of basing my approach form the information on this link. My idea was if I could use variables in the strcpy function and it would do all the character manipulation regarding the terminator character for me. I thought it was a function argument referencing issue I am having. I will need to get a much better handle on the use of pionters I can see it is a much more important issue here.

September 23, 2012
by Noter
Noter's Avatar

The problem was because 'incoming' is a single character so no null could be counted on to follow it in memory. If you made incoming a 2 character array and put a null in the 2nd character then you could use it in the string functions.

char incoming[2];

and initialize the null;

incoming[1] = 0;

then put the character read in the 1st byte

incoming[0] = uart_read();

now you can pass incoming as a string to strcat

strcat (WkDay, incoming);

It works but that is the long way around, your code is larger and it takes many more instructions to get the job done.

I coded for years VB also and I don't think I ever specified ByVal or ByRef. Sometimes I still catch myself thinking VB when writing a c program. Old habits are hard to break ...

It just takes a little practice with pointers c and you'll be crusing.

September 23, 2012
by Noter
Noter's Avatar

A major difference between VB and c is that VB did all your memory management for you but in c you have to do it all yourself. So all the memory for strings has to be specifically allocated ahead of time because in general the string (and other) functions only know an address, not a length.

September 23, 2012
by sask55
sask55's Avatar

Thanks again

Thankfully I am not in any hurry. I don’t do enough of either coding any more to be really comfortable with either one let along both. I really do not like the back and forth. That is one of the major reasons I have started to use C# on the PC side of things. I am trying to switch to one type of language and C type is the direction I am headed for now.

Post a Reply

Please log in to post a reply.

Did you know that you can see each keypress on a computer keyboard on an oscilloscope? Learn more...