I see my mistak , I mostly use array pointers , they dont have it
Printable View
I see my mistak , I mostly use array pointers , they dont have it
eh? Assuming you are talking about a C string, a null byte is appended to the end of it...Zitat:
Zitat von hallo007
When you use double quotes, a null byte is appended to the end of the array.
:o Yay! I spotted a moca!Zitat:
Zitat von Moca
Is there anything you guys I could suggest for me to be able to save a homebrew game with? (Besides a .txt?)
.sav file
*.anythingyouwantZitat:
Zitat von MrChaos
then u could have ur own save file format that stores all the vars
Just do something similar to,
Then just pick a convenient extension, like '.sav' as Anti has said.Code:typedef struct _GAME_STATE
{
int blah1;
int blah2;
char name[20];
char lala;
int level;
int start;
int health;
} SGameState;
int saveGameState(const char* path, SGameState* gs)
{
FILE* pFile = fopen(path, "rb");
fwrite(gs, sizeof(SGameState, 1, pFile);
fclose(pFile);
return 1;
}
int loadGameState(const char* path, SGameState* gs)
{
FILE* pFile = fopen(path, "rb");
fread(gs, sizeof(SGameState), 1, pFile);
fclose(pFile);
return 1;
}
(With error checking, of course.)
I'm not sure what this post actually means but I'm sorry if I didn't explain the problem well. As you know an array is a collection of common structures stored in sequential memory.Zitat:
I see my mistak , I mostly use array pointers , they dont have it
The string "hello all" should actually be stored in sequential memory as:
rather than:Code:[h] [e] [l] [l] [o] [ ] [a] [l] [l] [\0]
Where the square brackets are a single byte/char variable.Code:[h] [e] [l] [l] [o] [ ] [a] [l] [l]
As you can see the correct example requires the array to be 10 elements long rather than 9.
While a string could be stored in memory without the null char, it isn't a good idea. Many function rely on the null character to signify the end of a string, without it they would continue working past the end of the string reading or writing to random data, this could cause weird, hard to track bugs or complete crashes.
Hopefully this helps :)
I'm pretty sure that wont even compile.Zitat:
char hallo[5] = "hallo";
works 100% fine
I'd go with a binary format like _dysfunctional's example. While easier to understand, text format save files are often too easy for people to edit in order to cheat. With binary you can make it harder to modify the variables that are important to cheaters. Especially those with little knowledge of programming.Zitat:
Originally Posted by MrChaos
Is there anything you guys I could suggest for me to be able to save a homebrew game with? (Besides a .txt?)
I don't see how using "sprintf" and buffers can simplify this code:Zitat:
Zitat von Auraomega
orCode:printf("Sometext 2 Moretext");
to:Code:printf("%s %02d %s", myCharVariable1, myIntVariable, myCharVariable2);
keep using sprintf's and buffers for everything and i will keep a minimalist approach. Thanks!Code:char myCharVariable1[50], myCharVariable2[50]; int myIntVariable;
sprintf(myCharVariable1, "Sometext");
sprintf(myCharVariable2, "Moretext");
myIntVariable = 2;
sprintf(buffer, "%s %02d %s", myCharVariable1, myIntVariable, myCharVariable2);
printf("%s", buffer);
Plus it all depends on what you are doing and whether the text is static or not, and if it is not static of course you will need to use sprintfs to initialize the variables with the data you need.
So many things wrong with both of those, I don't know where to begin @@
don't nitpick i'm trying to get a simple point across not an entire encyclopedia of coding standards.Zitat:
Zitat von Archaemic
There's a difference between trying to write code that's clean and code that will compile. That won't even compile. Or if it will, it'll give you a mess of warnings and crash instantly when run.
Not nitpicking but i'll just say a couple of things i have noticed from the above posts:Zitat:
Zitat von califrag
1) As you noted, whether you use a buffer or not depends on if you plan on changing the contents of the string.
- If the string is constant over the entire duration of the program
just use a constant pointer to a string literal.
- If the string changes use a character array and modify the string
with strcpy/sprintf ect.
You could even just have constant string literals and a character pointer that changes which string it points to, it's up to personal preference, there is no wrong or right way.
Keeping things as straight foward as possible like you have is a good idea however :).
2) The string constants in both examples have type 'char', this is incorrect, they should either be a char pointer ('char*' or 'char variable_name[]') or a char array of the appropriate size (char variable_name[size]).
3) You cannot use sprintf to output into a string literal. sprintf is only used to write into a character array defined by the programmer. This is a common mistake, it is important to note that a pointer and an array are not the same thing, the name of an array is a constant pointer but the actual body is a memory space. Before sprintf'ing to a pointer make sure that it points to an array of memory big enough to hold the data.
Hope this helps :)
wow 150 pages into this thread i decide to open my mouth and try to offer some help about a stupid printf function that gerbils can understand. i'm not trying to write code for someone to copy paste into their program and compile immediately.. if i was i'd post the source to hello world. i was simply trying to demonstrate the many different ways that printf can be used WITHOUT sprintf or strcpy or buffers of any kind. the guy I was ORIGINALLY talking to wanted to put static text into his program.. anyways i've learned a lot from this thread.. don't offer any help unless you're just going to copy paste some source code for someone else to copy paste into their program. thanks.
??? Sorry to offend, just trying to help mate :)
no not you jono you actually kinda backed me up a little thanks, if you notice i made my code more "compileable" for those who are so nitpicky... :)Zitat:
Zitat von psp_jono
ok. im looking for function which would make from string:
hello all
just:
all
basicly, i need to cut away 1st 6 letters? i just cant remeber that function name..... string something
I need to know how i would go about doing some animation. Any links ?
The function you can use is strcpy in the header <string.h>.Zitat:
Zitat von myschoo
Use it like this -
What that does is copies the data in the string starting at 6 bytes in front of the address of the array until the null byte is reached. It copies it to the beginning of the string.Code:char hello[] = "hello all";
strcpy(hello,hello+6);
You would use the function like this - strcpy(destination,source );
I'll explain a bit more about arrays and whatnot if needed later. for now, I need sleep.
Ive been googling but still cannot find anything. Is there any source code i can obtain that will show me the basics and i can then build upon ?
animation? u can use spritesheet and change coordinates using timer or something similar
Moca: sweet thats what i was looking for. thx
A timer is the only way ?
With OSLib its pretty easy to make animations:)
Is OSLib completed for psp ?
btw thanks for the recommendation im reading up now.
Zitat:
Zitat von myschoo
Would also do the trick.Code:char helloStr[10] = "Hello all";
printf(helloStr); // Hello all
helloStr += 6;
printf(helloStr); // all
oslib is the most completed library in existance for the psp :)Zitat:
Zitat von eldiablov
I'm gonna throw in another one just for fun.:tup:Zitat:
Zitat von _dysfunctional
I am pretty sure that would workCode:char *textall;
char texthelloall[] = "Hello All";
textall = strstr(texthelloall,"All");
whats the diffrence between:
char *textall; and char textall;
One is a pointer and the other one isn't...
the first one creates a pointer to a char. the name of the POINTER is textall.Zitat:
Zitat von myschoo
the second on creates a char. the name of the CHAR is textall.
thx. i think i understand :)
How would one change the text under the title?Zitat:
Zitat von myschoo
Wait...you can't do that O_oZitat:
Zitat von _dysfunctional
I kinda see what you are tryin' to do but... You can't modify the address of an array o_O. Unless if I missed something huge.
not possible anymore.Zitat:
Zitat von Mr305
before, premium member could pay it for non-prem.
Your right, your right.Zitat:
Zitat von Moca
It would work as a pointer, but not as a plain array. My mistake.
Would have worked with the use of another pointer,Code:char helloStr[10] = "Hello all";
printf(helloStr); // Hello all
helloStr += 6;
printf(helloStr); // all
or without pointer arithmetic,Code:char helloStr[10] = "Hello all";
printf(helloStr); // Hello all
char* allStr = helloStr;
allStr += 6;
printf(allStr); // all
or without storing a pointer at all,Code:char helloStr[10] = "Hello all";
printf(helloStr); // Hello all
char* allStr = &helloStr[6];
printf(allStr); // all
Code:char helloStr[10] = "Hello all";
printf(helloStr); // Hello all
printf(&helloStr[6]); // all
jono wouldn't the last example only display the 6th character not up to the 6th character?
I'm fairly certain jono is right on all points in that post.
It would print the 6th character and every character following until a null ('\0') character is encountered :).Zitat:
Zitat von seenit
Heres another one:
I'm not a big fan of pointer arithmetic most of the time, it can make code a fair bit harder to understand. Although at times it does make sense to use it.Code:char helloStr[10] = "Hello all";
printf( helloStr); // prints "Hello all"
printf( helloStr + 6); // prints "all"
EDIT: null character was '\n' rather than '\0'. oops :o.
Ehh, not quite right on all points this time, jono. \n is the newline character. \0 is the null character.