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...
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
Ethan Gutierrez
Yeah, I think so... the assignment for the function was given to us like this.
Is there a specific way I'm supposed to set it up to get the data into the struct properly or something?
Grayson Cox
Looking at the prototype it seems the two struct pointer parameters are for output.
Mason Martin
please help
Justin Collins
but it's a void function. am I missing something?
Wyatt Morales
The function is supposed to be used as such Move w, b; parseNotationString(input, &w, &b);
Isaac Bell
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.
Brody Harris
Pointer parameters are used for two things in C: passing large input structures, and passing output parameters. A well known sample is scanf.
Carson Jenkins
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...
Justin Edwards
You don't use malloc in this case. The caller allocates memory.
Jacob Wright
Okay, yeah. That was actually just a leftover from when I was writing it as it's own main to test it out.
Zachary Sanders
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().
Isaac Wright
You don't need help allocation either way. Stack allocation is enough.
Oliver Taylor
It's kind of long... no doubt unnecessarily long... give me a minute
Levi Howard
just put it onto pastebin
Nicholas Cox
I'm afraid it'll get on google and my professor will call me out for plagiarism or some shit.
to skip over the part of the string that tells you which number turn it was in the chess game.
Gavin Murphy
but strtok(>>>NULL
Nolan Walker
I don't know... it hasn't given me any problems yet at least.
I learned most of this a few hours ago...
Carson Murphy
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.
Jack Rodriguez
What is the input anyway
Levi Gutierrez
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.
Oliver Davis
I just realized I probably should've posted the header file too.
while (last && a < 3) { last = parts[a++] = strtok(last, " "); }
char *wht = parts[1]; char *blk = parts[2];
what struct problem
Camden Russell
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.
Parker Hall
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
Cooper Kelly
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.
Daniel Richardson
this is one of the reasons i hate strtok. it stores the last pointer passed in and may cause mysterious problems.
Connor Ramirez
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...
Asher Turner
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?
James James
What the fuck, I didn't even change anything yet and now it's working out of the blue...
John Peterson
uwotm8
Robert Martin
forgot to recompile before the previous attempt?
John Jackson
I don't even know... was fucking codeblocks glitching out or something?!
I did absolutely nothing different...
Thomas Gray
i was about to tell you to use Visual Studio or Dev-C++
Xavier Baker
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.
Caleb Garcia
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.