Ok How would I fix the following? I'm clueless currently.
Ok How would I fix the following? I'm clueless currently.
Last edited by Strahd; 09-30-2011 at 07:18 AM.
Did you finally make your c strings terminated by a NULL character? If not then as you were told on your other post you cannot use strcat with them.
As for your code:
- If Xstr is a struct then you need to declare it as such in C, e.g. struct Xstr x, ect.
- Your strcat function is missing a ')'
What is the difference between Xstr and your local struct xstr?
This is not going to work, strlen expects a pointer to char as argument. What you need is:Code:otherSize = strlen(Xstr other);
Code:otherSize = strlen(other.string);You should use the . rather than the -> operator, since x is not a pointer. Goes for the rest of the code too.Code:(x->size) = newSize;
You should probably store the realloc in a temporary variable, since it can fail, and if it does your string pointer in x is lost forever. When you see that it succeeds you can assign it to x.string.Code:(x->string) = realloc((x->string),newSize);
strcat also needs a pointer to char, other should be other.string, and you are missing a ) in the end.Code:strcat((x->string),(other);
Last edited by iceaway; 09-30-2011 at 12:42 AM.
Not sure if I am following you on that? Xstr is an extendable string, and in .h file we were provided "typedef struct xstr * Xstr;" What I would like to do is be able to do, is put Xstr x; (or something like) in the function, but with the flags I am required to use the following happens "warning: ISO C90 forbids mixed declarations and code".What is the difference between Xstr and your local struct xstr?
What I am not understanding is other is another Xstr, from my perspective I do not know what is in there or what. So with the . operator, isn't that like saying or assuming other is stored in my struct string?This is not going to work, strlen expects a pointer to char as argument. What you need is:
Code:otherSize = strlen(other.string);
And got the rest thank you. As for that missing ), it wasn't missing int he code, I'm guessing while formatting the code for here, it got deleted or I didn't grab it when i cut and pasted.
Last edited by Strahd; 09-30-2011 at 01:12 AM.
So the code you recieved from your class typedef'd a pointer? Got it. Where do you go to school? I could always use a second job......
"typedef struct xstr * Xstr;" was what I was looking for. Then Xstr->string would be valid for accessing the string member of the struct. But what is the purpose of your local xstr struct? It is not used anywhere in the code you posted.
The warning means that you are declaring variables in the middle of a function. The ISO C90-standard mandates that all variables be declared at the top of the function, i.e.:
is not okay, since all declarations are not in the beginning. It would have to be changed into:Code:void fcn(void) { int a; a = 5; int b; }
Code:void fcn(void) { int a; int b; a = 5; }
Well in my local struct, I am storing information that I have other functions using. And initial thought the -> operation (ie x->size) was retrieving or storing information in my local struct. But by what you are sayign it seems like it wasn't."typedef struct xstr * Xstr;" was what I was looking for. Then Xstr->string would be valid for accessing the string member of the struct. But what is the purpose of your local xstr struct? It is not used anywhere in the code you posted.
Ahh ok I got it, and I see what was setting that off, now. Thank YouThe warning means that you are declaring variables in the middle of a function. The ISO C90-standard mandates that all variables be declared at the top of the function, i.e.:
You told us this earlier in another thread, but new problem, new thread, new information. Therefore, you should mention this in every new thread, or simply use struct xstr* instead.Originally Posted by Strahd
Last edited by laserlight; 09-30-2011 at 01:38 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
But I'm thinking this is part of my problem.
I know for typedefs, it makes it easier to work with. But in my case where the function is accepting (Xstr x, Xstr other), isn't that the same as if I went inside the function and did (breaking it down to there is only one).
I was answering Ice's question. And I understand where you are coming from.Code:#include <stdio.h> #include <string.h> #include <stdlib.h> struct foo { char *text; int length; int cap; }; typedef struct foo* magic; int main() { magic box; char *s = "this is a string"; box->text=s; ... }
Last edited by Strahd; 09-30-2011 at 02:08 AM. Reason: Added something else