![]() |
| | #16 |
| Registered User Join Date: Sep 2005
Posts: 34
| Code: char *a1 = "Available";
printf("%s\n",a1);
if(1){
a1 = "N/A";
}
printf("%s\n",a1);
|
| mrcheesypants is offline | |
| | #17 |
| Registered User Join Date: Sep 2004 Location: California
Posts: 2,845
| |
| bithub is offline | |
| | #18 |
| Registered User Join Date: Apr 2009 Location: Russia
Posts: 116
| 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) |
| c.user is offline | |
| | #19 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #20 | |
| Registered User Join Date: Apr 2009
Posts: 41
| Quote:
char a[5] = "ABCDE"; is junk. incorrect, and a very bad example. does that even compile with your compiler? If it does tell me which one. Last edited by strickyc; 04-28-2009 at 06:30 PM. | |
| strickyc is offline | |
| | #21 | ||
| Registered User Join Date: Apr 2009 Location: Russia
Posts: 116
| Quote:
I said if he does not use any functions from string.h he may delete this include from the code Quote:
![]() 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. | ||
| c.user is offline | |
| | #22 | |
| Registered User Join Date: Apr 2009
Posts: 41
| First off let me apologize to the OP for hi-jacking his/her thread. Quote:
Code: #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. | |
| strickyc is offline | |
| | #23 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition Last edited by BEN10; 04-29-2009 at 02:12 AM. | |
| BEN10 is offline | |
| | #24 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| i just compiled this piece of code and it gave me the error too many initializers Code: #include <stdio.h>
#include<conio.h>
int main(void)
{
int a[3]={1,2,3,4};
return 0;
}
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #25 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| its in my signature. visual studio 2005. and i dont think its a bad compiler at all.
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #26 | |
| Registered User Join Date: Apr 2009
Posts: 41
| Quote:
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. | |
| strickyc is offline | |
| | #27 | |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| Quote:
Code: Warning 1 warning C4045: 'a1' : array bounds overflow
__________________ HOPE YOU UNDERSTAND....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition | |
| BEN10 is offline | |
| | #28 |
| DESTINY Join Date: Jul 2008 Location: in front of my computer
Posts: 656
| ok, now i'm getting what's going on. If i try to run this code Code: char a1[5] = "ABCDE"; But if i run this code Code: char a1[5] = "ABCDEF"; in case of int array Code: 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....... for( ; ; ) printf("If you can't make it good, at least make it look good"); PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D. IDE- Microsoft Visual Studio 2008 Express Edition |
| BEN10 is offline | |
| | #29 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #30 | |||||
| Registered User Join Date: Apr 2009 Location: Russia
Posts: 116
| Quote:
Quote:
)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 )Quote:
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) Quote:
"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]) Quote:
quzah says right, because in C99 they didn't cancel this rule for initialization (only in C++ it can cause an error) | |||||
| c.user is offline | |
![]() |
| Tags |
| string |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help with comparing strings (I'm a beginner C programmer) | Bandizzle | C Programming | 2 | 04-29-2009 10:13 AM |
| beginner question about strings | gp364481 | C Programming | 4 | 09-05-2008 06:31 PM |
| beginner: dynamic array of strings | pc2-brazil | C++ Programming | 10 | 04-29-2008 04:29 PM |
| Strings Program | limergal | C++ Programming | 4 | 12-02-2006 03:24 PM |
| A beginner passing strings | jamjar | C Programming | 2 | 09-01-2002 12:50 AM |