Thread: Pointers and strncat

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95

    Pointers and strncat

    Hello,

    I'm trying to keep the firstLine char pointer to be independent from the spaces one but i don't manage to achieve that.

    Code:
    char* firstLine = spaces;
    strncat(firstLine,s,m-rm); //that's it now I want the actual block on memory to be copied.
    
    			s = malloc(sizeof(int)*(n-m)); j++;
    			for (k=0; k<n; k++){
    				s[k] = A[j]; j++;	  
    			} char* rString = s; A = spaces;
    			strncat(A,rString,n-rm);
    So by the end firstLine and spaces should point to different places in memory. How to achieve that?

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    7
    Can you be more specific in explaining your problem, the one you gave is kind of vague.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Well, lets look at this one then:

    Code:
    char* breakSmart(char* A, int n, int m, int* costs){
    	char* formatted = malloc(sizeof(char)); char* not = A; char* rFormat; char* tempNot; int j; int inLength, rLength;
    	if (n > m)
    		while ((inLength = length(not)) > m){
    			j = inLength;
    			while (not[j] != '\n' && not[j] != ' ') j--;
    			not[j] = '\n';rLength = inLength-j;
    			rFormat = malloc(sizeof(char)*rLength);  int r;
    			for (r=j+1; r<=inLength; r++)
    				rFormat[r] = not[r];
    }
    
    For some weir reason rFormat ends up with nothing, although not/A contains for them.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    no one to help on this here?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by simpatico_qa
    no one to help on this here?
    It would help if you formatted your code better, e.g.,
    Code:
    char* breakSmart(char* A, int n, int m, int* costs) {
        char* formatted = malloc(sizeof(char));
        char* not = A;
        char* rFormat;
        char* tempNot;
        int j;
        int inLength, rLength;
        if (n > m)
            while ((inLength = length(not)) > m) {
                j = inLength;
                while (not[j] != '\n' && not[j] != ' ') j--;
                not[j] = '\n';
                rLength = inLength - j;
                rFormat = malloc(sizeof(char) * rLength);
                int r;
                for (r = j + 1; r <= inLength; r++)
                    rFormat[r] = not[r];
            }
    So, it turns out that your code is invalid.

    Basically, you should post code that you attempt to compile, and also post what is the problem.
    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

  6. #6
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    with me it compiles but simply doesn't fill the char array. What makes it invalid to you?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by simpatico_qa
    What makes it invalid to you?
    The function is a fragment. You do not have the closing brace at the end, and you are missing the return statement (though that might result in a warning rather than an error, but is a mistake nonetheless).
    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

  8. #8
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    that's because i tried to post the code that is problematic. So if u close the brackets and put a return u'll still see that the rFormat is not filled. This is my problem.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    that's because i tried to post the code that is problematic. So if u close the brackets and put a return u'll still see that the rFormat is not filled. This is my problem.
    Okay, so you are saying that we can take this as the function implementation:
    Code:
    char* breakSmart(char* A, int n, int m, int* costs) {
        char* formatted = malloc(sizeof(char));
        char* not = A;
        char* rFormat;
        char* tempNot;
        int j;
        int inLength, rLength;
        if (n > m)
            while ((inLength = length(not)) > m) {
                j = inLength;
                while (not[j] != '\n' && not[j] != ' ') j--;
                not[j] = '\n';
                rLength = inLength - j;
                rFormat = malloc(sizeof(char) * rLength);
                int r;
                for (r = j + 1; r <= inLength; r++)
                    rFormat[r] = not[r];
            }
        return rFormat;
    }
    Now, what is your test input? What is the expected test output and actual test output?

    Remember, if n <= m, then rFormat will be left uninitialised. Also, by assigning to rFormat in a loop, it means that rFormat will only point to the memory allocated on the last iteration of the loop (and the memory allocated on all previous iterations will be leaked).
    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

  10. #10
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    There is something clearly absent from my mind:

    Code:
    int* init(){
    	int* costs = malloc(sizeof(int)*co);
    	int i=0;
    	while (*(costs+i) != '\0'){
    		costs[i] = INF;
    		i++;
    	} return costs;
    }
    Even this doesn't not enter the while loop. What's up?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The array is uninitialised, so you should not be accessing it like that (especially since the point of the loop is to initialise the array, is it not?)
    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

  12. #12
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    is not it initialized by default to 0? Would u be so kind to articulate ur suggestion in code so that i grasp it?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by simpatico_qa
    is not it initialized by default to 0? Would u be so kind to articulate ur suggestion in code so that i grasp it?
    No, it is not. You are probably thinking of calloc() instead of malloc(). But if you use calloc(), the loop will terminate at the start of the first iteration.

    What are you trying to do?
    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

  14. #14
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    just fill the costs array with a fixed no, say 34.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by simpatico_qa
    just fill the costs array with a fixed no, say 34.
    Right. Take a look at this example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    
    int *initCosts(size_t size, int initial_value);
    
    int main(void)
    {
        const size_t size = 10;
        const int initial_value = 34;
        int *costs = initCosts(size, initial_value);
        if (costs)
        {
            size_t i;
            for (i = 0; i < size; ++i)
            {
                printf("%d\n", costs[i]);
            }
        }
        else
        {
            puts("Sorry, could not allocate memory.");
        }
        return 0;
    }
    
    int *initCosts(size_t size, int initial_value)
    {
        int *costs = malloc(sizeof(*costs) * size);
        if (costs)
        {
            size_t i;
            for (i = 0; i < size; ++i)
            {
                costs[i] = initial_value;
            }
        }
        return costs;
    }
    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

Popular pages Recent additions subscribe to a feed