Anyone on here know C?

Anyone on here know C?

I'm having an issue with structures... Codeblocks won't let me use the dot operator in the function that I'm calling, I have to use an arrow, but it works just fine in main, and apparently this is causing the data that I'm passing to the struct to not go in the right place...

Any ideas on why this is happening?

Other urls found in this thread:

mediafire.com/download/p46mme4d56itu36/testcase03.c
mediafire.com/download/08wmf6jixp4cjcg/ChessMoves.h
twitter.com/SFWRedditImages

Are you passing a pointer to the struct into the function? If so then of course you have to dereference that pointer to get the struct itself, and (*p).m is the same as p->m

Yeah, I think so... the assignment for the function was given to us like this.

void parseNotationString(char *str, Move *whiteMove, Move *blackMove);

Is there a specific way I'm supposed to set it up to get the data into the struct properly or something?

Looking at the prototype it seems the two struct pointer parameters are for output.

please help

but it's a void function. am I missing something?

The function is supposed to be used as such
Move w, b;
parseNotationString(input, &w, &b);

Yeah, passing a pointer in as an argument means that you're writing to the data pointed to.

So you'd do something like:

void increment(int *n) {
*n += 1;
}

int main(void) {
int x = 1;
increment(&x);
printf("%d", x);
}

The point here is that the value of x in main is actually changed because you passed it by reference, not by value, to increment(). And inside increment(), you had to refer to the value as *n, not n.

Similarly, you're passing the Move structures by reference, as pointers. I assume when you call parseNotationString it looks like...

// str contains something.
Move whiteMove, blackMove;
parseNotationString(str, &whiteMove, &blackMove);
// now whiteMove and blackMove contain something.

Hope that helps! C's not the friendliest of languages.

Pointer parameters are used for two things in C: passing large input structures, and passing output parameters. A well known sample is scanf.

This shit is way over my head...

here's what's happening in main

in the function I wrote, I used malloc(sizeof(Move)) to get some space for whiteMove and blackMove, and once I translated the string, I started putting the values I got into the structs with arrow opperators...

You don't use malloc in this case. The caller allocates memory.

Okay, yeah. That was actually just a leftover from when I was writing it as it's own main to test it out.

Assuming the definition of Move doesn't require any allocations, yes; the memory for the two Move objects is stack-allocated in main().

OP, show us your implementation of parseNotationString().

You don't need help allocation either way. Stack allocation is enough.

It's kind of long... no doubt unnecessarily long... give me a minute

just put it onto pastebin

I'm afraid it'll get on google and my professor will call me out for plagiarism or some shit.

here's the .c file
mediafire.com/download/p46mme4d56itu36/testcase03.c

Line 77: strtok(NULL, " ")
What?

to skip over the part of the string that tells you which number turn it was in the chess game.

but strtok(>>>NULL

I don't know... it hasn't given me any problems yet at least.

I learned most of this a few hours ago...

strtok finds the next character that matches any character in the second parameter, an array, by iterating through the memory starting at the address provided by the first parameter, and returns a pointer to it. If it is NULL it will never find anything.

What is the input anyway

A chess notation string, in this case "18. R4xe4 h6"

the string gets passed over, and then I ended up using strcpy to make a copy of that string because strtok was crashing if I didn't, (probably because of what you said), and strtok breaks it up at the spaces so I can store the white and black moves in separate arrays and sort through each easier.

I just realized I probably should've posted the header file too.

mediafire.com/download/08wmf6jixp4cjcg/ChessMoves.h

why bother learning C when you can just script?

because university is a bitch.

heres a revised version of what you want to do

char *last = str;
char *parts[3];
unsigned char a = 0;

while (last /* != 0*/)
{
if (a > 2) break;
last = parts[a++] = strtok(last, " ");
}

char *wht = parts[1];
char *blk = parts[2];

eyy dubs

a slightly more concise version

char *last = str;
char *parts[3];
unsigned char a = 0;

while (last && a < 2)
{
last = parts[a++] = strtok(last, " ");
}

char *wht = parts[1];
char *blk = parts[2];

thanks!

does that fix the struct problem though?

fml

char *last = str;
char *parts[3];
unsigned char a = 0;

while (last && a < 3)
{
last = parts[a++] = strtok(last, " ");
}

char *wht = parts[1];
char *blk = parts[2];

what struct problem

main keeps printing out "failwhale ):" because the data isn't getting put into the Move struct properly.

I can't figure out how to get it in there.

but isn't this function supposed to be called once with the string in the first parameter, then with the NULL pointer instead? I'm confused

this is not OP btw

first of all ignore the last three snippets, here's the final one
char *last = str;
char *parts[3];
unsigned char a = 0;

while (last && a < 3)
{
last = parts[a++] = strtok(last, " ");
}

char *wht = parts[1];
char *blk = parts[2];

as for your failwhale problem, the cause is at line 31. it is outside a condition block and therefore is always called.

this is one of the reasons i hate strtok. it stores the last pointer passed in and may cause mysterious problems.

are you sure?

if you put a printf statement in main that prints the stuff in whiteMove, none of the values are in there.

but it's fine if the print statement is in the parse function...

I definitely did something wrong...

failwhale keeps getting printed is due to line 31 having failwhale() called outside of the if block. you don't have braces around the statement.
what is the current output you are getting from the console?

What the fuck, I didn't even change anything yet and now it's working out of the blue...

uwotm8

forgot to recompile before the previous attempt?

I don't even know... was fucking codeblocks glitching out or something?!

I did absolutely nothing different...

i was about to tell you to use Visual Studio or Dev-C++

I probably will after this shit... I could've gotten to sleep hours ago...

I'm gonna go die now, thanks for putting up with me and looking at my code.

np user
I write DirectX code, you haven't seen anything. Besides a lot of ppl at school who are way shittier that ask me simpler stuff than this so this is much better than shit. If you come from a world of managed code or scripting it is natural that you haven't seen pointers at all. Despite that it is most useful to know pointers and what you can do with them. Check out Microsoft's Component Object Model, which is what a large portion of Windows is built upon.