forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Strings??

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Dremth



Joined: 15 Jul 2005
Posts: 14
Location: Round Rock, TX

PostPosted: Sun Oct 29, 2006 12:01 pm    Post subject: Strings?? Reply with quote

How would I go about declaring strings in the PSPSDK? I've tried:
string blah;
but I get:
main.c:27: error: syntax error before 'blah'
main.c:27: warning: type defaults to 'int' in declaration of 'blah'
in the compiler. Is there an include file I need to recognize or is there a special type I need to use?

One more. How would I use strings in a printf()?

With integers it's like:
printf("%i", blahint);

so would it be:
printf("%s", blahstring);
?

Much appreciated!
Back to top
View user's profile Send private message
ReKleSS



Joined: 18 Jun 2005
Posts: 73
Location: Melbourne, Australia

PostPosted: Sun Oct 29, 2006 5:40 pm    Post subject: Reply with quote

You might want to brush up on your basic C skills...
You're after a character array.

Code:

char mystring[num];


Your formatting for the printf is correct. Note that your character array can also go in place of the formatting string.
Back to top
View user's profile Send private message
adrahil



Joined: 16 Mar 2006
Posts: 277

PostPosted: Sun Oct 29, 2006 7:40 pm    Post subject: Reply with quote

and don't forget to add a \n character whenever you want to print something out onto the screen, since the psp does some sort of output buffering on debug screen before showing it up... (made me go totally crazy at some point :P)
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Sun Oct 29, 2006 8:20 pm    Post subject: Re: Strings?? Reply with quote

Dremth wrote:
How would I go about declaring strings in the PSPSDK? I've tried:
string blah;
but I get:
main.c:27: error: syntax error before 'blah'
main.c:27: warning: type defaults to 'int' in declaration of 'blah'
in the compiler. Is there an include file I need to recognize or is there a special type I need to use?

One more. How would I use strings in a printf()?

With integers it's like:
printf("%i", blahint);

so would it be:
printf("%s", blahstring);
?

Much appreciated!


in C, "string" as a type doesn't exist :
Code:

char blah[] = "....";

...
printf("%s\n", blah);


in C++ :
Code:

#include <string>
#include <iostream>

std::string blah;

...
std::cout << blah << std::endl;
Back to top
View user's profile Send private message
Dremth



Joined: 15 Jul 2005
Posts: 14
Location: Round Rock, TX

PostPosted: Sun Oct 29, 2006 11:53 pm    Post subject: Reply with quote

I knew that I could use the char blah[]; to do a string like thing but I wanted a string array. Like string blah[];. Would I just do char blah[][];? Just do a double demension array?
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Mon Oct 30, 2006 12:56 am    Post subject: Reply with quote

Dremth wrote:
I knew that I could use the char blah[]; to do a string like thing but I wanted a string array. Like string blah[];. Would I just do char blah[][];? Just do a double demension array?


in C :
Code:

char str1[] = "...";
char str3[] = "...";
...

char *strarr[] = { str1, "blahblah", str3, ... };

printf("%s\n", strarr[1]) // prints "blahblah"


char blah[][] is impossible.

Please bear in mind :

int arr[2][3] : 2 arrays of 3 ints
int arr[][3] : N arrays of 3 ints
int arr[2][] : 2 arrays of N ints --> SYNTAX ERROR !

Note that :

char *arr[2] means an array of 2 POINTERS on c strings
Back to top
View user's profile Send private message
Dremth



Joined: 15 Jul 2005
Posts: 14
Location: Round Rock, TX

PostPosted: Mon Oct 30, 2006 2:27 am    Post subject: Reply with quote

When I try to do this:

char item[];
item[] = "blah";

I get this error:

main.c:2: error: syntax error before ']' token

Same happen whens I do this:

char item[3];
item[] = "blah";

But I get no errors when I do this:
char item[] = "blah";
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Mon Oct 30, 2006 2:42 am    Post subject: Reply with quote

This is normal, you don't seem to know either C or C++

please read a C language tutorial and you will know what you can do or not.

Because it is a non-sense to write : blah[] = "ABC." since it must be a declaration of new variable;

the only way to use [] in a expression is to access an element of the array, so :

char blah[] = "ABC"; // blah is a CONSTANT string being seen as an array of char.

printf("%c\n", blah[1]); ------> displays a B on screen.
Back to top
View user's profile Send private message
Dremth



Joined: 15 Jul 2005
Posts: 14
Location: Round Rock, TX

PostPosted: Mon Oct 30, 2006 3:13 am    Post subject: Reply with quote

I know C++ but not C. I just happen to not be very good with arrays.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group