![]() |
|
This thread is locked; no one can reply to it.
![]() ![]() |
1
2
|
Strings...The mystery |
wit
Member #858
November 2000
![]() |
OK. I'll try to explain my problem in a short way. I have a function in my game which asks the players name. It has a char array of 25 chars which is modified by inputing the name. The function returns the array. Another function which calls that function calls also strcpy() to copy the returned string to another string. ask_name(){ |
Daniel Micheal
Member #472
June 2000
|
wit, in C remember that strings are simply arrays of characters which are terminated by a null character of the value 0. If you have a variable called "char *player_name" that simply means its pointing to an array of characters. Its known as a dynamic array, as opposed by "char player_name[255]" which is static, and the length is non-changable. You cannot write past 255 characters. |
Bruce Perry
Member #270
April 2000
|
> char * player_name; -- |
Bruce Perry
Member #270
April 2000
|
I KNEW that would happen! Two answers (probably conflicting) at the same time! Read mine for the quick fix. Read CorsairK8's if you want to know what's going on. -- |
ReyBrujo
Moderator
January 2001
![]() |
Remember that declaring a variable static is nearly the same than declaring it global. So you can access the variable no matter where. I will recommend CorsairK8's way... just remember to set the pointer to NULL once you have freed the memory with "free". If you want to use BDavis' way, I would declare it global. Statics will only be "recognized" once the function was called, and that can create some problems if you try to access it before it is "created". void a_function() { -- |
amarillion
Member #940
January 2001
![]() |
Hehe, strings are always a bit tricky in C. This it the one and only thing BASIC is actually better at (or at least easier). -- |
vpenquerch
Member #233
April 2000
|
Just a bit of clarification: char *blah="hello, world"; is correct: this is a pointer that points to a string constant, and this data can not be modified. char blah[222]="hello, world"; is correct and is a pointer pointing to a non constant (eg you can strcpy to it) portion of memory. tell me if a goofed |
zaphire
Member #1,273
September 2000
![]() |
Aside: you can also find the apstring class, by searching for it on the 'net. This class allows you to implement strings very easily. (of course, this is only for C++) You could probably find some structs for C somewhere on the net, that could make the job of handling string a bit easier... ------- |
vpenquerch
Member #233
April 2000
|
or the stl string, if you want something portable and widely used. |
wit
Member #858
November 2000
![]() |
Thanks a lot guys! |
Daniel Micheal
Member #472
June 2000
|
Strings are very simple, and I think that there is no need for a library to handle string manipulation. It only slows things down when you can handle the string with the standard rtl string functions (string.h). Strings are very important in any kind of programming, you should learn how to use them and understand them since they are a fundamental of the c language. Not knowing how to use strings is like not understanding how to use a 'struct' or use classes. Amarillion: Basic is not better than C when using string. C allows you to dynamically allocate strings that are very large, and you can use as much memory as the computer has free. BASIC limits you to 64k maximum. There are alot of very bad limits to basic, not to mention that basic is very slow and impossible to create a game with it. There are exceptions, such as using libraries written in C or assembler (directx, and some other qbasic library support this) but the actual game itself is severly limited and can be incredibly slow. Granted, C can give you too much control sometimes and even cause you to crash the computer when doing something incorrectly (its happen to me many times when accessing memory im not allowed to, or dividing by zero, probably two of the most common problems) |
wit
Member #858
November 2000
![]() |
Ahmm.By the way when we are talking about structures... WIT |
Jason Heim
Member #484
June 2000
|
wit, struct SCORE high_scores[10]; and then to modify an entry: high_scores[0].credits = 10; if you want it to be an array of pointers, then you must allocate a SCORE struct to each pointer: struct SCORE * high_scores[10]; after that, each entry holds a unique pointer to memory that is SAFE to use struct SCORE zero_me; or if you want to set just the name to all blanks, you can do this: memset(&(zero_me.name), ' ', sizeof(zero_me.name)); you have to remember that a pointer is just four bytes of memory, like a long int. all it stores is an address, and until you set that address to something useful with 'malloc' or by assigning it to allocated storage, it will most likely point to storage that you don't want to use. |
Jason Heim
Member #484
June 2000
|
aw hell, i messed up a bit. this line: memset(&(zero_me.name), ' ', sizeof(zero_me.name)); is wrong, the '&' is not necessary. please ignore that line, it's a bad example of what i was trying to show. here are two ways to code it correctly: /* hard way */ what's worse though is that by setting all the bytes of a character array to non-NULL, i'm creating an unterminated string. you shouldn't set strings like i did in the above at all. i apologize for the confusion, i get overzealous in my wordy explanations |
wit
Member #858
November 2000
![]() |
Thanks again! |
ReyBrujo
Moderator
January 2001
![]() |
To make things easy, don't put * in front of the structure and use it as an array. Ah, Sikobabel, the "hard form" isn't that hard. In fact, it's easier to explain someone that, to access a determinated position of a string, you must use the &string[0], &string[1], &string[2]... Once he gets used to it, then you can explain that "&string[0]" and "string" are the same grin Finally, you can explain that string[2] and *(string + 2) are the same, but some compilers make better code with the second one wink RB -- |
Bruce Perry
Member #270
April 2000
|
Does anyone know if DJGPP GCC and/or Ming GCC recognise the optimisation in the first one? -- |
mingles
Member #1,002
February 2001
|
Hmmmmmmmmm thats a tuffy! I suggest that you take a giant dump on your keyboard that should fix it. If not then piss between the keys and lick my mammas arse! |
Jason Heim
Member #484
June 2000
|
mingles, %hanks a lo%!, now my k#yboard do#sn'% s##m %o work %h# way i% us#d to! plus, %h# %as%# on my %ongu# is v#ry disgus%ing... pl#as# %ry %o b# mor# h#lpful n#x% %im#
|
Jason Heim
Member #484
June 2000
|
rey, char label[10]; for instance, on some platforms with strict compilers you cannot say: pointer = label + 2; and the really strict ones won't even let you do: pointer = label; but every compiler i've seen will let you use the 'hard way' notation: pointer = &label[0]; and every compiler will let you treat these the same: pointer = &pointer2[0]; the only difference is that 'label' is treated by some compilers as, well, a label instead of a 4-byte storage unit for pointers (or 8, 16 bytes on 64, 128-bit systems). as such, occasionally compilers will get fussy about how they are used. |
mingles
Member #1,002
February 2001
|
I am writing to you to apologise from the heart of my bottom for posting such bad language on this forum (especially to WIT) I believe that this is the correct code to type in your program: ask_name(){ |
wit
Member #858
November 2000
![]() |
To mingles: |
Bruce Perry
Member #270
April 2000
|
And back to my question: Does anyone know if DJGPP GCC and/or Ming GCC recognise that string[2] can be optimised to *(string+2) ? That's Ming short for MingW32, not Mingles. -- |
Tom St Denis
Member #972
February 2001
|
Um string[2] and *(string+2) are the same thing. They should compile to similar if not identical code. |
Bruce Perry
Member #270
April 2000
|
They should, but do they? -- |
|
1
2
|