C style strings, pointers, chars etc.

I'm struggling to get my head around C style strings, pointers, chars etc.

In the following bit of code i believe i've created a C-stlye string. So, const char *strName; is a pointer to a bit of memory that contains a string? In the next line, i declare strName = "Hello World";. But is "strName" a pointer? Or a char? I'm really confused. The code works, but i don't quite understand what it is i've created!
I thought a pointer has to point towards a memory address that contains an int, double, char or whatever. But in this code it seems to me the only object created is the pointer?!
Or is the creation of const char strName implicit when i declare: const char *strName?

1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include <iostream>

int main()
{
	const char *strName;
	strName = "Hello World";
	std::cout << strName << "\n";
}
Last edited on
strName is pointing to the first element in the string "Hello World". If you try printing *strName you will see that it gives you the 'H'.
So, const char *strName; is a pointer to a bit of memory that contains a string?

Not quite. It's a pointer, but it's uninitialized and doesn't point to anything, except perhaps invalid memory.

But is "strName" a pointer?

Yes. "Hello Word" is C-string that's stored in memory somewhere. Line 7 assigns the address of where "Hello Word" is stored and places that address in your pointer.

But in this code it seems to me the only object created is the pointer?

There are two object involved here. Your uninitialized pointer and the C-string created by the compiler.

Or is the creation of const char strName implicit

No it's not implicit because you did not assign anything to it (until you get to line 7).


Thanks guys, this forum is fantastic!

AbstractionAnon said:
There are two object involved here. Your uninitialized pointer and the C-string created by the compiler.


So the "Hello World" string is only accessible via the strName pointer? If i (somehow) delete the pointer, i've lost the string forever, right?
Is it generally better to use std::string to create strings? C-style strings a bit old fashioned and convoluted, maybe? Or do they have their uses?

The following code form Learncpp.com uses the C style string (line 12), but it seems to me it would be simpler to replace it with std::string:

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
38
39
40
41
42
#include <iostream>
#include <cstring> // for strncpy()
 
class Employee
{
public:
    char m_strName[25];
    int m_nID;
    double m_dWage;
 
    // Set the employee information
    void SetInfo(const char *strName, int nID, double dWage)
    {
        strncpy(m_strName, strName, 25);
        m_nID = nID;
        m_dWage = dWage;
    }
 
    // Print employee information to the screen
    void Print()
    {
        using namespace std;
        cout << "Name: " << m_strName << "  Id: " << 
            m_nID << "  Wage: $" << m_dWage << endl; 
    }
};
 
int main()
{
    // Declare two employees
    Employee cAlex;
    cAlex.SetInfo("Alex", 1, 25.00);
 
    Employee cJoe;
    cJoe.SetInfo("Joe", 2, 22.25);
 
    // Print out the employee information
    cAlex.Print();
    cJoe.Print();
 
    return 0;
}

Last edited on
Yes using std::string is simpler and there is less risk of making mistakes that lead to bugs. I think you should always prefer std::string instead of C style strings unless you have a very good reason.
Ok, thanks!
Topic archived. No new replies allowed.