C type strings Syntax check

Okay, I was making sure all of my beginner concepts were polished, (and so you will be seeing stupid beginner questions quite often), so I was playing around with C type strings (apparently C++ has a different string class).

Before posting here I read all them from the search bar above. What I don't seem to understand is the syntax for strcat and strncat...(They are almost similar)

Why does the pointer version in line 14 works sometimes, and what does it exactly mean?
Also, why does the line 15 version (it is for strncat, yes, but the strcat one works the same way)

I commented out the other ones since I (think) I understand them, but if you have something to offer, it would be most appreciated.

Also, what is up with strcoll? How is it different from strcmp?

{If you have any other sugggestions on what kind of strings I should be using in the future, then please tell. }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<limits>
#include<string.h>
using namespace std;

 int main()
 {
 char name1[]="First";
 char name2[]=" Second";
 char name3[]=" Fourth";
 char name4[]=" Fourth";
 
 //strcat
 char* strcat(char* name1, char* name2);
 strncat(name3,name4,4);
 cout<<name1<<endl;
 cout<<name3;

 
 /*strcpy
 strcpy(name1,name2);
 cout<<name1;
  
//strcmp
int i = strcmp(name3,name4);  
cout<<"\t"<<i;

//strlen
cout<<"\t"<<strlen(name1);
*/ 

std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return 0;
  }
	
Last edited on
char* strcat(char* name1, char* name2);
This is declaring that a function exists, named strcat. It is NOT calling a function.


{If you have any other sugggestions on what kind of strings I should be using in the future, then please tell. }

Use C++ strings.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<string>
#include<limits>
using namespace std;

 int main()
 {
 string name1="First";
 string name2="Second";
 string name3="Third";
 string name4="Fourth";
 


// concatenation
string name5 = name1 + name2;
 cout<<name5 << endl;

 // copy
name5 = name3;
  
// compare
string name6 = "Third";
if ( name3 == name6 ) cout << "Same" << endl;


//length
cout<< name1.length() << endl;


std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return 0;
  }
You claim that you have read http://www.cplusplus.com/reference/cstring/strcat/
but admit that you did not understand it.

A possible implementation:
1
2
3
4
5
6
7
8
9
10
11
12
13
char* strcat( char* destination, const char* source )
{
  char* dst = destination;
  while ( dst ) ++dst;

  while ( source ) {
    *dst = *source;
    ++dst; ++source;
  }

  *dst = '\0';
  return destination;
}

The strncat is similar, but the second loop has extra condition:
1
2
3
4
5
  int count = 0;
  while ( count < num && source ) {
    *dst = *source;
    ++dst; ++source; ++count;
  }


Description of both functions says:
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string, including the additional null-character.

Your example program does not supply large enough destinations to the functions. Your fault.
OKay,

TO keskiverto
So I should have used something like
 
char name[40] = "First";

?
I foolishly assumed that [] automatically adjusts for any extra space.
(Gotta learn vectors)

TO Repeater
This is declaring that a function exists, named strcat. It is NOT calling a function.

So, I generally don't need to use it? Can you specify an example where I might need to use it?

Thanks for the string class explanation.

On a side note, can someone explain why some strings need to be const at certain places?
What is a string literal?

Anything potentially wrong with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class person
	{
		public:
			person(char s[])
			{
				name=new char[strlen(s+1)];
				strcpy(name,s);
			}
			void print()
			{
				cout<<"My name is "<<name<<endl;
			}
		protected:
			char*name;
	};
So, I generally don't need to use it? Can you specify an example where I might need to use it?

Whenever you want to inform the compiler that a function exists, without providing the full implementation right there. This is typically done in header files. We call it "function declaration".


Anything potentially wrong with this:

Very much. It's using dangerous c-style arrays of char instead of a proper C++ string, and it's also using manual memory management which is bad, and there's a memory leak as well. It's just plain bad C++ code that basically C code forced into a C++ class.
This is typically done in header files. We call it "function declaration".


I know this! I was asking; since the person who wrote the string header file already did that for me, so unless I am making libraries, I don't need to do anything right?

It's just plain bad C++ code that basically C code forced into a C++ class.


I figured as much. The class was a part of a program: part of our Computer Science practicals given in the textbook. It didn't work as intended, and the instructor provided no useful insight.

>>So, unless you have something else to say, I am marking this as solved.<<
This is hilarious(ly bad):
1
2
3
char* s = ...
char* name = new char[ strlen(s+1) ];
strcpy( name, s );

The s is a pointer. s+1 is a pointer too.

If s = "Hello", then s+1 points to "ello".
There strlen(s) returns 5 and thus strlen(s+1) returns 4.
The strcpy( name, "Hello" ) requires that name has 6 elements.

strlen(s) + 1 is different than strlen(s+1).
Topic archived. No new replies allowed.