I already know that ;) Im saying, Zetta thought other wise... Not to point fingers I mean, im just saying, does C++ not allow this or what? Im just curious.
Printable View
I already know that ;) Im saying, Zetta thought other wise... Not to point fingers I mean, im just saying, does C++ not allow this or what? Im just curious.
It does. So what's the problem if some don't know this? Some at some point don't know how to avoid a loop being infinitely allocating memory, but they will learn. It would be different if there were a huge group of people claiming that & is not an operator, but a conjunctor and therefore insisting that C and C++ have bugs.
Also giving names with that is hugely childish (even when saying your not wanting to point fingers... sarcasm is no protection). You better edit your post.
Ya, a simple yes or no answer was all I needed Raphael. Excuse me for wanting to clairfy an answer to a question and getting bashed for it. Sometimes I think you act childish Raphael... You think I'm annoying so you find any little hole in a statement and exploit it... I highly doubt ZettBlade cares that I used his name as we had a simple 5 word discussion on it last night, and I just wanted to know from a reliable source.
It was no bash, it was a statement that I find it inappropriate to talk names with abviously wrong assumptions to ridicule those persons, although the wrong assumption is pretty meaningless.
As I said, I just suggest you edit your post to comment out the name. It has nothing to do with the original question, which was already answered anyway.
EDIT-PS: And yes, I may sometimes act childish in some way too, I admit. That's most likely pretty much human nature to do every then and when, but I try not to ridicule people without a reason (I hope everyone sees enough reason for my signature though :P)- and therefore I didn't say a name with my allusion, because the person in charge should know he is being talked about.
Is there a 'dofile' sort of command in C? Like if I had a .txt file with this:
How would I call these variables?Code:variable1 = 7
variable2 = 4
variable3 = 9
You'd have to parse the files and set the variables yourself.
I believe Dark_Alex's custom firmware POC had some code in it to parse a config file like this. Check it out.
Err?Zitat:
Zitat von homer
Wouldn'tresult in z==3, y==1, x==1? IIRC, 'var=value' resolves to '1', not 'value'. Or... am I in desperate need of a C refresher here?Code:int x,y,z;
x=y=z=3
x, y, and z will all equal 3.Zitat:
Zitat von FreePlay
I'm not sure I've ever seen that in Kernighan and Ritchie, but it does work that way.
Edit: Try resolving 'x=0' and see if you get a '1' out of that. ;)
That was it! Thank you!Zitat:
Zitat von Raphael
Refresh I would say ;) == is the comparison operator resolving to 1 if var is equal to value and zero else. x=y=z=1 is best read from right to left, ie z gets set to 1, then y gets set to z (=1) and x gets set to y (=z=1).Zitat:
Zitat von FreePlay
As for the send-struct-via-tcp-socket I have a whole lotta questions:
1.) How do I send a struct via a socket?
?Code:send(sock, (char)myStruct, sizeof(myStruct), 0);
2.) Can I also send an array of myStructs?
3.) If so, can I send structs that contain strings (char-arrays) of variable lenght? Like
4.) If all that sending-jazz really works; How do I convert the sent string back to a struct-array?Code:typedef struct {
char * name;
int num_albums;
char ** albums;
} artist_entry;
artist_entry * artist_list;
artist_list = (artist_entry *)malloc(num_artists*sizeof(artist_entry));
for(i=0; i<num_artists; i++)
{
[...]
artist_list[ i ].name = (char*)malloc(artist_name_lenght);
[...]
}
send(???);
5.) What was the point of the universe, again?
Thank you
Luke
Zitat:
Zitat von Lukeson
more likely. Typecasting a struct into a char is a bad idea generally. I'm not sure about the second parameter of the send command, but if it expects a pointer, you'd need to use &myStruct.Code:send(sock, myStruct, sizeof(myStruct), 0);
Yes, as long as the array is one single allocation (as in objection to an array of pointers to structs).Zitat:
2.) Can I also send an array of myStructs?
You can, but you will not submit the contents of the array, but only the pointer which in turn is useless on the client. So actually no, you cant. You need to make your structs non dynamic, without pointers for this to work as you want.Zitat:
3.) If so, can I send structs that contain strings (char-arrays) of variable lenght? Like
Code:typedef struct {
char * name;
int num_albums;
char ** albums;
} artist_entry;
artist_entry * artist_list;
artist_list = (artist_entry *)malloc(num_artists*sizeof(artist_entry));
for(i=0; i<num_artists; i++)
{
[...]
artist_list[ i ].name = (char*)malloc(artist_name_lenght);
[...]
}
send(???);
As said in the prior post, as long as the above requirements fullfill (well, even if they don't fullfill, it would work the same way, but when you'd try to use the struct on the client, you would get a crash).Zitat:
4.) If all that sending-jazz really works; How do I convert the sent string back to a struct-array?
42Zitat:
5.) What was the point of the universe, again?
No Problem :PZitat:
Thank you
Luke
Buh. I'm going insane. Time to re-teach myself C. Sigh.
Yes it is (using malloc).Zitat:
Zitat von Raphael
I was afraid that was the case. Isn't there a bad 'hacking'-way to actually copy the strings to where they are needed? Aren't there functions that allocate the memory where it's needed, to create strings in the place?Zitat:
Zitat von Raphael
I can't even use char name[length_of_the_longes_arti stname] in windows (can I?) so I'll probalby have cut some names. That sucks big time.
oh, I forgot about that. Thanks for enlighting meZitat:
Zitat von Raphael
-= Double Post =-
That's not neccissarily the worst thing to do, do what makes you happy...Zitat:
Zitat von FreePlay
-= Double Post =-
I think I'll go with my "send and parse a formatted string"-solution. Even though the memory allocation takes an ugly second.Zitat:
Zitat von Raphael
Only possibility would be to make your name and albums list a fixed array of maximum size (depending on how many artists, albums per artist max you want to support) and then transfer that. It will have a big overhead though (memory and therefore transfer size), especially when a lot of artists only have 1 or 2 albums actually so the rest of the array(s) is filled with zeros.Zitat:
Zitat von Lukeson
In your case that's prolly the best way to go.Zitat:
I think I'll go with my "send and parse a formatted string"-solution. Even though the memory allocation takes an ugly second.
Ok, does anyone have an idea why this only goes till artist 516 of 517 and, only at the 516th artist work only 4 of 5 albums?
----------------------------------------= Double Post =----------------------------------------Code:memcpy(num_artists, &artist_data, 5);
num_artists[5]='\0';
offset = 5;
printf("num artists: %d\n", atoi(num_artists)); // num artists: 517
int i,j;
for(i=1; i<=atoi(num_artists); i++)
{
memcpy(artist_name_lenght_c, &artist_data[offset], 3);
artist_name_lenght_c[3]='\0';
artist_name_lenght = atoi(artist_name_lenght_c);
offset += 3;
artist_name = (char *)realloc(artist_name, artist_name_lenght+1);
memcpy(artist_name, &artist_data[offset], artist_name_lenght);
artist_name[artist_name_lenght]='\0';
offset += artist_name_lenght;
memcpy(num_albums_c, &artist_data[offset], 4);
num_albums_c[4]='\0';
num_albums = atoi(num_albums_c);
offset += 4;
printf("artist #%d: %s (%d), %d albums:\n", i, artist_name, artist_name_lenght, num_albums);
if(num_albums==0) num_albums=1;
for(j=1;j<=num_albums;j++)
{
memcpy(album_name_length_c, &artist_data[offset], 3);
album_name_length_c[3]='\0';
album_name_length = atoi(album_name_length_c);
offset += 3;
album_name = (char *)realloc(album_name, album_name_length+1);
memcpy(album_name, &artist_data[offset], album_name_length);
album_name[album_name_length]='\0';
offset += album_name_length;
printf(" %s (%d)\n", album_name, album_name_length);
}
}
I'm having more troubles:
Errormessage: line 104: pspwamp_commands.c:104: error: invalid use of undefined type 'struct artist_entry'
Spoiler for Whole error-list:
Code:
pspwamp_datatypes.c
main.cCode:[...]
typedef struct {
char * name;
int num_albums;
char ** albums;
} artist_entry;
[...]
pspwamp_commands.cCode:[...]
artist_entry* artist_list;
artist_list = (artist_entry *)malloc(sizeof(artist_entry));
get_artists_from_server(sock, artist_list);
[...]
Does anyone have an idea?Code:int get_artists_from_server(int socket, struct artist_entry * artist_list)
{
[...]
artist_list = (artist_entry *)realloc(artist_list, atoi(num_artists)*sizeof(artist_entry));
[...]
int i,j;
for(i=0; i<atoi(num_artists); i++)
{
[...]
artist_list[i].name = (char*)malloc(artist_name_lenght+1); //l.104
[...]
}
}
Try from
toCode:typedef struct {
char * name;
int num_albums;
char ** albums;
} artist_entry;
http://www.netalive.org/codersguild/posts/1750.shtmlCode:typedef struct artist_entry_struct {
char * name;
int num_albums;
char ** albums;
} artist_entry;
Hm, doesn't work, I get the same errors. Why are there actually 2 names (artist_entry_struct and artist_entry)? That kinda confuses me...Zitat:
Zitat von head_54us
Which was why I left a link ;).. You have the error because the datatype artist_entry is not defined in pspwamp_commands.c. You have defined it in pspwamp_datatypes.c but pspwamp_commands.c knows nothing about this file.Zitat:
Zitat von Lukeson
Put the artist_entry defintion in a header file with header guards and include it where you need to use that datatype.
Did I write pspwamp_datatypes.c up there? It's pspwamp_datatypes.h! The whole code is
and it's included in main.c and in pspwamp_commands.cCode:#ifndef PSPWAMP_DATATYPES
#define PSPWAMP_DATATYPES
typedef struct artist_entry_struct {
char * name;
int num_albums;
char ** albums;
} artist_entry;
#endif
Gah.. I hate structs (I am a C++ man)
Either use
orCode:typedef {
char * name;
int num_albums;
char ** albums;
} artist_entry;
int get_artists_from_server(int socket, struct artist_entry * artist_list)
{
[...]
artist_list = (artist_entry *)realloc(artist_list, atoi(num_artists)*sizeof(artist_entry));
[...]
int i,j;
for(i=0; i<atoi(num_artists); i++)
{
[...]
artist_list[i].name = (char*)malloc(artist_name_lenght+1); //l.104
[...]
}
}
Code:typedef struct artist_entry_struct{
char * name;
int num_albums;
char ** albums;
} artist_entry;
int get_artists_from_server(int socket, artist_entry * artist_list)
{
[...]
artist_list = (artist_entry *)realloc(artist_list, atoi(num_artists)*sizeof(artist_entry));
[...]
int i,j;
for(i=0; i<atoi(num_artists); i++)
{
[...]
artist_list[i].name = (char*)malloc(artist_name_lenght+1); //l.104
[...]
}
}
You are typedef'ing your struct therefore you don't need the "struct" specifier on the parameters. Read up on typedefs again. You have two possibilities:
Change all "struct artist_entry" parameters to "artist_entry" only, or change your declaration into "struct artist_entry { ..... };"
EDIT: Heh, basically the same is written on the site from headus' link earlier I just noticed. Read that page, seriously.
Works now, thank you!
Same here, but my fist tries using C++ on the PSP failed miserably!Zitat:
Gah.. I hate structs (I am a C++ man)
-= Double Post =-
PS: Does anyone have an explanation for my first problem?
http://forums.qj.net/f-psp-developme...e138.html#1376
Ive got a problem with the TIFF USB, when I run the application it wont activate the USB Port for it. Any help?
This is so wierd, when I use for(i=1; i<515; i++) he prints all 514 artists and their albums, but after the function has finished (I print 'Closing connection') right after it, he prints another 3 albums! That's impossible!Zitat:
Zitat von Lukeson
And if I say for(i=1; i<=514; i++) he only does 26 albums! This is crazy...
Is your program multithreaded?
I use threads, but I only start 1 thread...
If you mean, you have the main thread and start a new thread, your program is multithreaded and explains why you see the status of the socket closing before the function finishes.
One thread is dealing with printing the artists/albums (and is given it faster then it can print it), the other thread closes the socket.
His problem was that it printed stuff that really shouldn't be there (the three albums), I believe atleast.Zitat:
Zitat von head_54us
Hmm.. hell, if all else fails, debug the damn thing. Step through the code and check the data as it is recieved.
I have three backgrounds in an Images/Backgrounds/ directory:
Back1.png
Back2.png
Back3.png
I tried using an array but that didn't work, and heres how I load it:
My currentBack variable:Code:Image* Background = loadImage("./Images/Backgrounds/Back1.png");
And heres where supposadly when I pres the Right Trigger its suppose to switch:Code:int currentBack = 1;
It just stays the same, and it wont ever switch!! Ive tried almost everything here! And heres where I try to blit my Background variable to the screen:Code:if((pad.Buttons & PSP_CTRL_RTRIGGER) && (currentBack == 1))
{
Background = loadImage("./Images/Backgrounds/Back2.png");
currentBack = currentBack+1;
}
if((pad.Buttons & PSP_CTRL_RTRIGGER) && (currentBack == 2))
{
Background = loadImage("./Images/Backgrounds/Back3.png");
currentBack = currentBack+1;
}
if((pad.Buttons & PSP_CTRL_RTRIGGER) && (currentBack == 3))
{
Background = loadImage("./Images/Backgrounds/Back1.png");
currentBack = 1;
}
Help!Code:blitAlphaImageToScreen(0, 0, 480, 272, Background, 0, 0);
You sure you called flipScreen(); right after the blitAlphaImageToScreen(.. .)?
EDIT: Oh, I thought it always stayed black...
Create an array of 3 Images and load them all up beforehand.
In your button press check just change the currentBack variable.
In your blitting function just use the position in the array ie.
You also should initialise the currentBack variable to 0, as arrays are indexed from 0 to limit-1.Code:blitAlphaImageToScreen(0, 0, 480, 272, Background[currentBack], 0, 0);
Seems more logical to load all 3 images, and switch the background according to the variable... If var = 1, have background 1blit, if var=2, have background 2 blit, etc...
What's faster? For an iPod-style menu I need to draw those little arrows
http://bombach.info/luke/mainmenu.png
Is it faster to blit each arrow to the screen (the single small image), or blit one big image with all arrows to the screen, not showing the arrows where there's no menuitem? (Was that understandable?)
-= Double Post =-
I assume the latter is faster. Also I need to leave a bit of the screen black.
http://bombach.info/luke/mainmenu2.png
What's the fastest way to do that? Drawing a white rectangle and leaving it black? I heard that is pretty slow. Is there a better way?
You should try out more than asking questions for everything you want to do. It's not hard implementing both methods for both cases and checking wich is faster.
Drawing rectangles is faster than drawing textures (images), as long as you do it with GU.
These are my first steps in C, I'm still very 'careful', wanting to have confirmed every step I take. But I must confess I could have answered these questions myself, I didn't really think before writing them (a behavoir which I always which I always gripe PHP-noobs about...). :/ SorryZitat:
You should try out more than asking questions for everything you want to do.
EDIT: Never mind... I was just being stupid... >_<
Well, I have no problem with that, because I don't have to answer your questions :P It's only that you will learn much more, when you try around and find a solution/answer yourself (because you also learn things that don't have to do with the actual solution - as in this case you would know how to do the imperfect solution too, plus how to bench the code)
true true...