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";
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.
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.
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
constchar* str = newchar[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 = newchar[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 = newchar[30]; // reallocate memory to the the pointer. perfectly allowed.
I hope that clarifies the differences between the two. =)
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 constchar* str = newchar[20];
For char str[20];, you have space for 20 characters be set aside, to be known by the name `str'.
For constchar* str = newchar[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).
#include <iostream>
usingnamespace 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
constchar* str = newchar[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 = newint;
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.