Thread: Debugging Help w/strlen() and strcat()

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    111

    Debugging Help w/strlen() and strcat()

    Ok How would I fix the following? I'm clueless currently.
    Last edited by Strahd; 09-30-2011 at 07:18 AM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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:
    1. If Xstr is a struct then you need to declare it as such in C, e.g. struct Xstr x, ect.
    2. Your strcat function is missing a ')'
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Strahd View Post
    Ok How would I fix the following? I'm clueless currently.

    file.c: In function 'XstrAddXstr':
    error: expected expression before 'Xstr'
    warning: passing argument 2 of 'strcat' from incompatible pointer type
    What is the difference between Xstr and your local struct xstr?

    Code:
    otherSize = strlen(Xstr other);
    This is not going to work, strlen expects a pointer to char as argument. What you need is:
    Code:
    otherSize = strlen(other.string);
    Code:
    (x->size) = newSize;
    You should use the . rather than the -> operator, since x is not a pointer. Goes for the rest of the code too.
    Code:
    (x->string) = realloc((x->string),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:
    strcat((x->string),(other);
    strcat also needs a pointer to char, other should be other.string, and you are missing a ) in the end.
    Last edited by iceaway; 09-30-2011 at 12:42 AM.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    What is the difference between Xstr and your local struct xstr?
    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".

    This is not going to work, strlen expects a pointer to char as argument. What you need is:
    Code:
    otherSize = strlen(other.string);
    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?

    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.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    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......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Quote Originally Posted by Strahd View Post
    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".


    And got the rest thank you.
    "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.:
    Code:
    void fcn(void)
    {
        int a;
        a = 5;
        int b;
    }
    is not okay, since all declarations are not in the beginning. It would have to be changed into:
    Code:
    void fcn(void)
    {
        int a;
        int b;
        a = 5;    
    }

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    "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.
    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.

    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.:
    Ahh ok I got it, and I see what was setting that off, now. Thank You

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Strahd
    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;"
    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.
    Last edited by laserlight; 09-30-2011 at 01:38 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    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).

    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; 
       ...
    }
    Quote Originally Posted by laserlight View Post
    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.
    I was answering Ice's question. And I understand where you are coming from.
    Last edited by Strahd; 09-30-2011 at 02:08 AM. Reason: Added something else

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another strlen
    By voidX in forum C Programming
    Replies: 15
    Last Post: 01-09-2011, 11:24 PM
  2. strlen help please
    By LuizCPFL in forum C Programming
    Replies: 7
    Last Post: 12-10-2008, 04:04 AM
  3. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 12:15 PM
  4. strlen()
    By BoneXXX in forum C Programming
    Replies: 6
    Last Post: 06-17-2007, 01:32 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 01:34 PM