Strings??

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Dremth
Posts: 14
Joined: Fri Jul 15, 2005 9:41 am
Location: Round Rock, TX

Strings??

Post by Dremth »

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!
User avatar
ReKleSS
Posts: 73
Joined: Sat Jun 18, 2005 12:57 pm
Location: Melbourne, Australia

Post by ReKleSS »

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

Code: Select all

char mystring[num];
Your formatting for the printf is correct. Note that your character array can also go in place of the formatting string.
adrahil
Posts: 274
Joined: Thu Mar 16, 2006 1:55 am

Post by adrahil »

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)
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Re: Strings??

Post by hlide »

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: Select all

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

...
printf("%s\n", blah);
in C++ :

Code: Select all

#include <string>
#include <iostream>

std&#58;&#58;string blah;

...
std&#58;&#58;cout << blah << std&#58;&#58;endl;
Dremth
Posts: 14
Joined: Fri Jul 15, 2005 9:41 am
Location: Round Rock, TX

Post by Dremth »

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?
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

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: Select all

char str1&#91;&#93; = "...";
char str3&#91;&#93; = "...";
...

char *strarr&#91;&#93; = &#123; str1, "blahblah", str3, ... &#125;;

printf&#40;"%s\n", strarr&#91;1&#93;&#41; // 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
Dremth
Posts: 14
Joined: Fri Jul 15, 2005 9:41 am
Location: Round Rock, TX

Post by Dremth »

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";
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

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.
Dremth
Posts: 14
Joined: Fri Jul 15, 2005 9:41 am
Location: Round Rock, TX

Post by Dremth »

I know C++ but not C. I just happen to not be very good with arrays.
Post Reply