Pointer Dereferencing

I just started studying pointers yesterday and am confused on a line of code. First off, here is the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main()
{
	int *p;
	char *name;
	string *str;

	p=new int;
	*p=28;

	name=new char[5];

	strcpy(name, "Johh");

	str=new string;
	*str="Sunny Day";
	
	return 0;
}


On line 16, shouldn't it be strcpy(*name, "John") because I thought name (without the dereferencing operator) can only store an address and not a c-string, or any other data for that matter.
http://www.cplusplus.com/reference/clibrary/cstring/strcpy/

strcpy has been written to accept a pointer. The function takes that pointer and does the necessary dereferencing for you.

Which compiler are you using that doesn't need #include <string> to know what a string is?
Last edited on
Ok cool. I am using Visual C++ 2008 Express Edition. I didn't realize I didn't include that header but somehow the compiling still succeeded... although there was a problem with strcpy that I am not sure about:

------ Build started: Project: Project, Configuration: Debug Win32 ------
Compiling...
Source1.cpp
c:\users\kevin\documents\visual studio 2008\projects\project\project\source1.cpp(16) : warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
c:\program files (x86)\microsoft visual studio 9.0\vc\include\string.h(74) : see declaration of 'strcpy'
Linking...
Embedding manifest...
Build log was saved at "file://c:\Users\Kevin\Documents\Visual Studio 2008\Projects\Project\Project\Debug\BuildLog.htm"
Project - 0 error(s), 1 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
That's just a warning, telling you that it's easy to go wrong with that function and suggesting you consider using strcpy_s instead.
That's just Microsoft deprecating a few slightly unsafe features and offering their own to replace them.

You need to make sure on your own (I think) that the destination points to at least enough memory to hold all of the string pointed to by source. Otherwise, you'll have some difficulty making the copy. ;)

-Albatross
the microsoft compiler lets you get away with alot. ive noticed you dont have to include limits for many things as well. string seems ok to declare it without the string header but if you try and use it with cout it will fail to compile without using the right header.
Topic archived. No new replies allowed.