'Learning CPP' programmer here. So, bear with me.

First off :
STRCAT :

http://www.cplusplus.com/reference/clibrary/cstring/strcat/

When clearly the definition says :

char * strcat ( char * destination, const char * source );

Why'd they use char str[80] in the example???
What types can be used?

Recommended Answers

All 2 Replies

str[80] just means: an array of 80-chars. They just chose 'more then enough' characters. 100 would also be fine, or 30.

If you're serious about learning C++, then drop the characterarray as soon as possible. std::strings are the way to go:

#include <iostream>
#include <string>

int main (){
    std::string str1 = "this is string 1";
    std::string str2 = "this is string 2";
    std::string total = str1 + " and " + str2;
    std::cout << total; // output the string to screen
}

The code above is C++, the code you posted is actually C (which will also run in C++, but it's not recommended)

Why'd they use char str[80] in the example???
What types can be used?

It sounds like you may be confused as to how an array/C-style string is passed to a function in C and C++. In the example, the array for a C-style string str is declared.

/* strcat example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[80];
  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  puts (str);
  return 0;
}

When the function strcat is called, it is passed str . Is this the point of confusion?

The array as passed not by value (meaning a copy of the entire array), but rather as a pointer to the first element of the array. The str in this case is a sort of 'shorthand' for &str[0] , which is a char* -- as expected by strcat .

You may also want to wander through this section of the comp.lang.c FAQ.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.