You're probably better off using just a character pointer.
Code:char *a1 = "Available"; printf("%s\n",a1); if(1){ a1 = "N/A"; } printf("%s\n",a1);
You're probably better off using just a character pointer.
Code:char *a1 = "Available"; printf("%s\n",a1); if(1){ a1 = "N/A"; } printf("%s\n",a1);
strickyc, if he is copying strings by functions which wait a '\0', he must know: 1) he may catch a problem with an array size (segfault) 2) he may catch a trouble with a function if it will not get a '\0' (trash in effect string or segfault)
None of that has anything to do with using incomplete character arrays and your statement that he can't use string.h functions with them. You were wrong, we aren't. That pretty much sums up everything in this thread beyond your first post.
Quzah.
Hope is the first step on the road to disappointment.
Last edited by strickyc; 04-28-2009 at 06:30 PM.
where did I say it ? show meOriginally Posted by quzah
I said if he does not use any functions from string.h he may delete this include from the code
it is correct codeOriginally Posted by strickyc
go and check, because it is correct initialization of a char array and its compiling is OK with a -Wall and gives only unused variable ‘a’
Last edited by c.user; 04-29-2009 at 12:29 AM.
First off let me apologize to the OP for hi-jacking his/her thread.
error line 5: initializer-string for array of chars is too longCode:#include <stdio.h> int main(void) { char a1[5] = "ABCDE"; return 0; }
Like I said name the compiler that it compiles on.(the one you used)
so I know which one to avoid.
Edit: I think your think that a[5] has six elements to it bu it doesn't. it ony has 5 elements.
they are a[0],a[1],a[2],a[3],a[4] they is no actual a[5] in a[] a5 would be a[5-1] which is a[4] which is the fifth element of the array which in a array of characters the last element is reserved for the '\0' char. You can later over write the '\0' but IMO would be somewhat foolish if you plan on using the standard library to manipulate the char array.
Maybe your compiler allows this I don't know, But I would think it's not a good compiler if it does.
Can you name the compiler or what IDE it came with?
Last edited by strickyc; 04-29-2009 at 01:52 AM.
Last edited by BEN10; 04-29-2009 at 02:12 AM.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
i just compiled this piece of code and it gave me the error
too many initializers
plz someone tell me why is this error not shown in char arrays?Code:#include <stdio.h> #include<conio.h> int main(void) { int a[3]={1,2,3,4}; return 0; }
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
I havent used visual studios well enough to know it. maybe they just consider it a logical error so left it out. they is no reason to say a[6] = "ABCDE" when a[ ] = "ABCDE" is much clearer.
This would work with my compiler I think. But I knew user.c example wouldn't, Didnt even have to test it to see. But I did it to prove it.
Code:char a[5] = {'A', 'B', 'C', 'D', 'E'};
so does your complier compile
char a1[3] = "reallylotsofstuff"
Last edited by strickyc; 04-29-2009 at 02:18 AM.
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
ok, now i'm getting what's going on. If i try to run this code
it runs without any errors and warnings.Code:char a1[5] = "ABCDE";
But if i run this code
it gives me warning : array bounds overflowCode:char a1[5] = "ABCDEF";
in case of int array
the compiler gives me error: too many initializersCode:int a1[5] = {1,2,3,4,5,6};
can someone tell me why is this all happening? i'm confused by the behaviour of the C language.in the very first case, is the compiler ignoring the NULL character?
HOPE YOU UNDERSTAND.......
By associating with wise people you will become wise yourself
It's fine to celebrate success but it is more important to heed the lessons of failure
We've got to put a lot of money into changing behavior
PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
IDE- Microsoft Visual Studio 2008 Express Edition
Yes, it's ignoring the NULL. The reason you're getting an error or warning is possibly because it's compiling as C++. It's illegal in C++. For some reason, it lets you get away with this when using character arrays, even though for all other data types it's an error to have more initializers than you have elements.
Question 11.22
Quzah.
Hope is the first step on the road to disappointment.
no, I don't think that, it is from a book K&ROriginally Posted by strickyc
)Originally Posted by strickyc
it is a good compiler (all compilers conform to standards of the language, if it doesn't, it is not a compiler of a language, because it is a compiler of a half-language )
why do you think so ?Originally Posted by strickyc
I can use any function to manipulate the char array if it does all right (I can create my own function and use it in all program)
when you initialize an array of 5 elements by 6 elements it does a mistakeOriginally Posted by BEN10
"ABCDE" has 6 elements (we don't see a null-character)
but initialization a[5] = "ABCDE" puts 5 elements in the array and '\0' doesn't put anywhere
and initialization a[5] = "ABC" puts 4 elements in the array A, B, C, '\0'
it is rule for the initialization only of char arrays
and initialization a[] = "ABCDE" creates an array for all 6 elements (from a[0] to a[5])
it's ok (no null-character)Originally Posted by strickyc
quzah says right, because in C99 they didn't cancel this rule for initialization (only in C++ it can cause an error)