0

What is the difference between char* c, and char *c? What is wrong in this code?

#include<iostream>
#define num 2
using namespace std;
int main (){
    char *c="HelloWorld!!!";
    cout<<*(c+num);
    return 0;
}
9
  • Your compiler should have asked you to add a const at the declaration of c.
    – Bob__
    Commented Jun 18, 2020 at 5:32
  • 1
    What do you mean & char *c?
    – 김선달
    Commented Jun 18, 2020 at 5:37
  • What is wrong in this code? Beside of the missing const, there is nothing wrong with the code. It outputs l (Demo on coliru) which is the third letter in "HelloWorld!!!" (i.e. the character at c + 2). What did you expect? Commented Jun 18, 2020 at 5:39
  • If you want to print the string literal from the third letters until the end, you should remove the * in front of c+num. Commented Jun 18, 2020 at 5:41
  • 1
    As the answers have noted, there is no difference between char *c and char* c - but even so STICK WITH THE FORMER ! The * belongs to the variable, not the type. For example, if you have char* c,d,e, then only c is the char* .... d and e are of char only.
    – racraman
    Commented Jun 18, 2020 at 6:02

4 Answers 4

1

in c++, string literal is implemented by const char* . Additionally, the compiler will not distinguish

char* c;

and

char *c;

as they have same meaning (pointer to char)

0

If you compile your program with the -Wall flag enabled, you'll see the following error message:

warning: ISO C++11 does not allow conversion from string literal
      to 'char *' [-Wwritable-strings]
    char *c="HelloWorld!!!";

you should make the c variable a const char * in order to make the warning disappear since the string literal is implemented as such type, differently from C where char* would suffice.

Note: You could simply use the auto keyword and let the compiler find the right type:

auto c = "HelloWorld!!!"
0

There is absolutly no difference between char* c and char *c the problem with your code is that "HelloWorld" is a const char* so you either have to cast it to a char* or store it in a const char variable. Also I wouldn't define that macro, just store that number in a variable(constexpr variable if you want compile time evaluation) it's much easier to read

#include<iostream>
using namespace std;
int main() {
    constexpr int num = 2;
    char* c = (char*)"HelloWorld!!!";
    const char* c2 = "HelloWorld!!!";
    cout << *(c + num);
    return 0;
}
4
  • 1
    ; is missing in int num = 2.
    – K.R.Park
    Commented Jun 18, 2020 at 5:38
  • 1
    Technically "HelloWorld!!!" is a const char[14], not a const char*, although it can be implicitly converted to a const char*.
    – eesiraed
    Commented Jun 18, 2020 at 5:40
  • And I suggest using constexpr int num = 2; , if the #define was placed intentionally to mean compile-time-evaluated constant.
    – K.R.Park
    Commented Jun 18, 2020 at 5:41
  • num should be in a constexpr variable. Commented Jun 18, 2020 at 5:45
0

There is no difference between char* and char *, but there is difference between char& and char*, so please edit your question and be more specific, say what you are expecting to happen, because your code is doing what you wrote, so I don't know if maybe want to concatenate array of chars with 2. If this is the case, than I will suggest you to use <string>, in this library you can use operator+() for concatenation reference.

What you are doing in your code is: you create array, and then you print something on address c + 2. That is 'l'. When I run I get this. As someone sayed before you must be careful because const char* can not be used to initialize char*, reference.

If you really want to change that string you can do something like this:

 #include<iostream>
 #include <stdlib.h>

 #define num 2

using namespace std;
int main() {
    const char* c = "HelloWorld!!!";
    char* str;
    int n = 0;
    while (true)
    {
        if (c[n++] == '\0') // '\0' is character for terminating string
        {
            break;
        }

    }
    str = (char*)calloc(n + 1, sizeof(char));
    for (int i = 0; i <= n; i++)
    {
        if (i < n - 1)
        {
            str[i] = c[i];
        }
        else
        {
            if (i == n - 1)
            {
                str[i] = (char)('0' + num);// with this sum you wil get ascii code of '2' and write it in str + n - 1
            }
            else
            {
                str[i] = '\0';
            }
        }
    }
    cout << str;

    //cout << *(c + num);
    return 0;
}

There is a difference between string and char*. In C/C++ you can only have array of chars. You can perform any primitive operation on char as you can on int. But you can't perform operation directly on array if you don't call your own function. For that reason you should use libraries to perform operations such as concatenation, comparing strings, or something else. If you prefer to work directly with char* you should use <string.h>, but if you want to be more comfortable you can use <string> and create string as object.

Not the answer you're looking for? Browse other questions tagged or ask your own question.