diff

what is the difference between 'char' and "char"
What do you mean? Can you give a usage example?
actually i have to test something, i have to provide some input so for that i need some combination . like my understanding is that "char" is a string
Alright, if I correctly understand what you mean, "char" is indeed a string (a C string, as it's usually called). For example:
 
const char *s="char";

'char' doesn't mean anything. Single quotes are used for single character literals, as in 'c' or 'A'.
thanks helios ....means i have to us 'char' i have to use it like
char *s = " 'char' ";
m i getting it right
If you declare a variable of type char,
char mycharacter;
You can assign a character value to it using single quotes:
mycharacter = 'a';

If you declare an array of characters (a string),
char mystring[25];
You use double quotes to assign a string of characters up to a max of the length declared:
mystring = "More than one character";

Last edited on
If you declare an array of characters (a string),
char mystring[25];
You use double quotes to assign a string of characters up to a max of the length declared:
mystring = "More than one character";


that's not entirely correct, you can assign it upon initialization like so

 
char myString[25] = "More than one character";


but if you don't initialize it you can't do:

mystring = "More than one character";

instead you'll need to use the string functions strcpy

strcpy(myString, "More than one character");

or strncpy

strncpy(myString, "More than one character", 25);

It's better to use strncpy cause it's safer since you pass in the length of the array to avoid overflow.
Last edited on
Yes I made a mistake, while trying to make a simple point.

You cannot assign a "string constant" to a previously declared "char" string in the manner
I described. The types are incompatble.

You can assign it during initialization as Dancaster13 stated above:
char myString[25] = "More than one character";
What if i would initialize a char string of size 10 with a string having 12 characters.
As

char str[10]="this is char";

A c string always end with a '\0' character. But in this case what would be assigned to the str[9] element. How Overflow would make to initialize the string.

if you're initializing the character array you don't need to specify the size.

char str[] = "this is char";

is perfectly legal and the size will automatically be set to the exact number of characters needed by your character array. You can't resize it afterwards though.
Last edited on
can't we do like this
char *str = "this is char";
Is there really a difference? I wonder what
In char str[] = "this is char";
str is a array
and in char *str = "this is char";
str is a char pointer

In char str[] = "this is char";
str is a array
and in char *str = "this is char";
str is a char pointer


lolz, no offense but I found that a bit funny. =)

arrays are actually pointers as well. If I remember correctly though the only difference between an array and a pointer is that an array is constant. Which means that an array of characters such as

char str[20];

is actually equivalent to

const char* str = new char[20];

So the main difference between a pointer and an array is that an array is a constant pointer. So once you've set it to point to a memory address you can't change what it points to. You can still change the value inside the memory address that it points to, but you can't change the address itself. =)

so you can't do:

str = new char[30]; // trying to reallocate the array? compiler-error

but if it was a pointer then that would be allowed:

1
2
char *str = "this is a string";
str = new char[30];     // reallocate memory to the the pointer. perfectly allowed. 


I hope that clarifies the differences between the two. =)
Last edited on
closed account (1vRz3TCk)
arrays are actually pointers as well. If I remember correctly though the only difference between an array and a pointer is an array is constant. Which means that an array of characters such as
An array is NOT a pointer.


char str[20]; is actually Not equivalent to const char* str = new char[20];


For char str[20];, you have space for 20 characters be set aside, to be known by the name `str'.

For const char* str = new char[20];, you have space for a pointer set aside, to be known by the name `str' and it is set to point to an unnamed space for 20 characters that is set aside on the heap (its location is actually irrelevant).
Last edited on
well I'm not going to argue about that.

but if they're not pointers why would it be perfectly legal to access an array using a dereference operator like the following?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
    int array[10];
    
    for(int i = 0; i < 5; i++)
    {
        *(array + i) = i;
    }
    
    for(int i = 0; i < 10; i++)
    {
        cout << array[i] << endl;
    }
    cin.get();
    return 0;
}


I'm not closed to the idea that it isn't a pointer though but I sure would like to see explanations why it is NOT a pointer.

Edit:

Well I admit that

const char* str = new char[20];

is not equal to

char str[20];

but I think arrays are still indeed pointers else you can't use the dereference operator on them. arrays would be pointers to memory in the stack though, compared to normal pointers where you can point to addresses in the stack like so:

1
2
int x = 10;
int *ptr = &x;


or addresses on the heap like:

 
int *ptr = new int;


Not to mention you can assign an array to a pointer and use the array braces syntax on pointers.

1
2
3
4
5
6
7
8
9
10
11
12
int array[5];
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;

int *ptr = array;
for(int i = 0; i < 10; i++)
{
    cout << ptr[i] << endl;
}


so in my opinion that establishes that arrays are indeed pointers. I actually learned that stuff from C programming not C++. But since C++ was taken from C then I think it should be the same thing.
Last edited on
Arrays behave exactly like pointers, except in one crucial detail that makes them different from them:
1
2
T array[n];
array++; //illegal 
Arrays are not lvalues.

if they're not pointers why would it be perfectly legal to access an array using a dereference operator
Arrays are implicitly convertible to pointers. Thus *array and *(T *)array mean the exact same thing.
@helios thanks for the explanation, that helps clarify it. =)
Last edited on
@Dacster13--------it's ok i m just new in c, So u can laugh as much as u want but just help me to clear my doubt


By the way thanks to all to paint my wall with pointers and array .....thanks again for your help
@vgaur25 well I'm not really laughing at you actually, I just thought the way you described it was a bit funny.

As an analogy it's like if you ask someone what the difference between C++ and Java is and they answer that the difference is the name.

Anyways, I'm glad this topic was able to clear your doubts. It was also able to answer some questions for myself. =)
Last edited on
Topic archived. No new replies allowed.