C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-28-2009, 04:48 PM   #16
Registered User
 
Join Date: Sep 2005
Posts: 34
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);
mrcheesypants is offline   Reply With Quote
Old 04-28-2009, 04:52 PM   #17
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
Originally Posted by mrcheesypants View Post
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);
And now we're back to the very first response to this thread.
bithub is offline   Reply With Quote
Old 04-28-2009, 06:14 PM   #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   Reply With Quote
Old 04-28-2009, 06:21 PM   #19
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 04-28-2009, 06:24 PM   #20
Registered User
 
Join Date: Apr 2009
Posts: 41
Quote:
Originally Posted by c.user View Post
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)
The code you did as an example probaly won't compile in any good compiler so it was total rubbish

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   Reply With Quote
Old 04-29-2009, 12:25 AM   #21
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
Quote:
Originally Posted by quzah
your statement that he can't use string.h functions with them.
where did I say it ? show me

I said if he does not use any functions from string.h he may delete this include from the code

Quote:
Originally Posted by strickyc
char a[5] = "ABCDE";

is junk. incorrect, and a very bad example.
does that even compile with your compiler?
it is correct code
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   Reply With Quote
Old 04-29-2009, 01:38 AM   #22
Registered User
 
Join Date: Apr 2009
Posts: 41
First off let me apologize to the OP for hi-jacking his/her thread.


Quote:
Originally Posted by c.user View Post




it is correct code
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’
Code:
#include <stdio.h>

int main(void)
{
   char a1[5] = "ABCDE";

   return 0;
}
error line 5: initializer-string for array of chars is too long

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   Reply With Quote
Old 04-29-2009, 01:45 AM   #23
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by strickyc View Post
First off let me apologize to the OP for hi-jacking his/her thread. My last post in it.




Code:
#include <stdio.h>

int main(void)
{
   char a1[5] = "ABCDE";

   return 0;
}
yes a1 has 5 characters in it with NULL being the last one.
error line 5: initializer-string for array of chars is too long

Like I said name the compiler that it compiles on.(the one you used)
so I know which one to avoid.
i copy pasted ur code above and it compiles with 0 errors and 0 warnings. But looking at ur error it looks right coz a1 has 5 characters in it. What is the problem which is causing this?
__________________
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   Reply With Quote
Old 04-29-2009, 01:49 AM   #24
DESTINY
 
BEN10's Avatar
 
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;
}
plz someone tell me why is this error not shown in char arrays?
__________________
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   Reply With Quote
Old 04-29-2009, 01:55 AM   #25
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by strickyc View Post
First off let me apologize to the OP for hi-jacking his/her thread.


Can you name the compiler or what IDE it came with?
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   Reply With Quote
Old 04-29-2009, 02:13 AM   #26
Registered User
 
Join Date: Apr 2009
Posts: 41
Quote:
Originally Posted by BEN10 View Post
its in my signature. visual studio 2005. and i dont think its a bad compiler at all.
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.
strickyc is offline   Reply With Quote
Old 04-29-2009, 02:24 AM   #27
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by strickyc View Post
I havent used visual studios well enough to know it. maybe they just consider it a logical error so left it out.

so does your complier compile

char a1[3] = "reallylotsofstuff"
yes this time also my compiler compiles it successfully but with a warning
Code:
Warning	1	warning C4045: 'a1' : array bounds overflow
also if i try to access any element after the array bound it gives me run time error.
__________________
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   Reply With Quote
Old 04-29-2009, 02:38 AM   #28
DESTINY
 
BEN10's Avatar
 
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";
it runs without any errors and warnings.
But if i run this code
Code:
char a1[5] = "ABCDEF";
it gives me warning : array bounds overflow
in case of int array
Code:
int a1[5] = {1,2,3,4,5,6};
the compiler gives me error: too many initializers
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   Reply With Quote
Old 04-29-2009, 02:38 PM   #29
+++ OK NO CARRIER
 
quzah's Avatar
 
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   Reply With Quote
Old 04-30-2009, 09:51 PM   #30
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
Quote:
Originally Posted by strickyc
I think your think that a[5] has six elements
no, I don't think that, it is from a book K&R

Quote:
Originally Posted by strickyc
But I would think it's not a good compiler if it does.
)
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:
Originally Posted by strickyc
but IMO would be somewhat foolish if you plan on using the standard library to manipulate the char array.
why do you think so ?
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:
Originally Posted by BEN10
can someone tell me why is this all happening?
when you initialize an array of 5 elements by 6 elements it does a mistake

"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:
Originally Posted by strickyc
Code:
    char a[5] = {'A', 'B', 'C', 'D', 'E'};
it's ok (no null-character)

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   Reply With Quote
Reply

Tags
string

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:23 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22