Thread: Join two chars from array into a variable

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    27

    Join two chars from array into a variable

    I then plan to store the variable as a hex value. Cross that bridge later.

    Code:
    printf("\nTest data is: %c %c\n", testData1[y], testData1[s]);
    
    		strcpy (str, &testData1[y]);
    		strcat (str, &testData1[s]);
    Test data print out is working out exactly what I want. This loops, as it loops I need to store each joined variable as hex, then move on to next pair in the loop.

    strcpy and strcat isn't behaving as I'd hoped. What am I missing or looking for?

    Cheers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile with increased warning levels. Your compiler should be giving you a hint. What is it, and what do you understand by it?
    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

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Am I going about it the correct way?

    It tells me the stack around str is corrupt or something. I'm not on it at the moment

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mrapoc
    It tells me the stack around str is corrupt or something.
    That sounds like a runtime error. I'm talking about a warning at compile time.
    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

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    It was buffer overflow.

    I have modified it a bit. Still not correct, but I hope you can see what I'm trying to do

    Code:
    for (int i = 0; i <= p; i=i+2)	{
    		int y = i;
    		int s = i+1;
    
    
    		printf("\nTest data is: %c %c\n", testData1[y], testData1[s]);
    
    
    		strcpy (str, &testData1[y]);
    		strcat (str, &testData1[s]);
    		strcat ("0x", str);
    
    
    		*(hex + t) = "0x" + *str;
    		int t = t+1;	
    								
    	}
    So testdata[y] and testdata[s] are fine. I want to join [y] and [s] so that the variable str=[y][s]

    Then I want to make it so str=0x[y][s]

    Then add it to hex position

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is certainly wrong:
    Code:
    strcat ("0x", str);
    The first argument to strcat must be a pointer to a character in an array of char that can be modified. Modifying a string literal results in undefined behaviour.

    Rather:
    Code:
    strcpy(str, "0x");
    strcat(str, &testData1[y]);
    strcat(str, &testData1[s]);
    Note that you will be overwriting str on each iteration of the for loop.

    Furthermore, I suspect that you don't want to use strcat in this way, since you print testData1[y] and testData1[s] as chars, and y and s are consecutive. If so, you probaby want to write:
    Code:
    strcpy(str, "0x");
    strncat(str, testData + i, 2);
    str[4] = '\0';
    Thus you do away with y and s.

    Next, what are you trying to do in these two lines?
    Code:
    *(hex + t) = "0x" + *str;
    int t = t+1;
    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

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    Sorry I should be clearer.

    Code:
    int testData(void){
        unsigned char *hex;
        int t = 0;
        char *str;
        
    
    
    
    
        hex = (unsigned char *)malloc(65553);
        if (hex == NULL)
        {
            printf("Cannot allocate memory \n\r");
            exit(1);
        }
    
    
        /*store data*/
        char testData1[65] = "BDB653CC0800FD1018FD101AFD101CFD101EBDB665CC0002DD007F00067F0010";
            
        int x = sizeof(testData1) / sizeof(char);
        int p = x-2;
    
    
        for (int i = 0; i <= p; i=i+2)
        {
            int y = i;
            int s = i+1;
    
    
            printf("\nTest data is: %c %c\n", testData1[y], testData1[s]);
    
    
            strcpy (str, &testData1[y]);
            strcat (str, &testData1[s]);
            strcat ("0x", str);
    
    
            *(hex + t) = "0x" + *str; 
    //I know this is wrong, I'm trying to store the hex values now stored in str, and store them as hex in the hex+1, hex +2 locations
            int t = t+1;    
                                    
        }
    
    
    }
    So Y and S is my way of getting the separate pairs from the char array. Probably not the best way but I understand it.

    Each time the loop...loops i want to store STR into the hex pointer location. t increments each time so each pair is stored in an incremental location.

    I will be processing each pair later on in my program, hence why i need them in hex pairs.

    Thanks for the help
    Last edited by mrapoc; 07-06-2012 at 04:02 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mrapoc
    i want to store STR into the hex pointer location
    This is vague, but it sounds like you want to interpret a string like "0x10" as the number 16 (i.e, 0x10), then store that number into the current unsigned char. If so, then you should be writing in the loop body something like:
    Code:
    int t = 0;
    for (int i = 0; i <= p; i += 2)
    {
        char str[3];
        strncpy(str, testData + i, 2);
        str[2] = '\0';
        hex[t++] = (unsigned char)strtoul(str, NULL, 16);
    }
    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
    Jun 2005
    Posts
    6,815
    There are several problems with your code mrapoc (apart from the fact it doesn't come close to doing what you intend). Any of the problems individually yield undefined behaviour. All of them together .... well you have multiple instances of undefined behaviour, and sensible practice is to have none.

    1) str is an uninitialised pointer, not an array. The call of strcpy() and the first call of strcat() are therefore copying data to a random area of memory. You need to initialise str, for example, by using a malloc() call or make it an array of sufficient length.

    2) strcpy() and first strcat() expect to receive a C-style string (an array terminated with a char of value zero) as the second argument. Giving the address of an array element does not achieve that.

    3) As laserlight has said, strcat() assumes it can write to its first argument (write data past the end). A string literal, such as "0x", cannot be written to. The result of the writing to something that cannot be written to is undefined behaviour. Some compilers will warn about this.

    4) The addition operator does not append strings. The line "*(hex + t) = "0x" + *str" is setting hex[t] to some random value.

    5) The line "int t = t + 1" creates a new variable named t as something distinct from the previous variable named t, since it is a different scope. Technically, the code gives undefined behaviour, since it is initialised in terms of itself. In practice, it probably has no effect. It certainly does not increment the t defined near the top of your function.


    In C, it should not be necessary to typecast the result of a malloc() call. If you can't get your code to compile without the typecast, it either means you have forgotten to #include <stdlib.h> or that your C compiler is really a C++ compiler. A C++ compiler would refuse to compile code that passes "0x" as the first argument of strcat() though.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    27
    I'll give that a go. One of those occasions when you know what you want to achieve, but cannot get your head around how to do it

    I'm using visual studio c++ to do my ansi c compiling etc.

    Probably not a good thing to do though right?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Using Visual Studio is fine as long as you have it configured to compile as C.

    By the way, notice that I declared str in the loop. You don't have to do that, but you should declare it as an array, not a pointer, unless you want to dynamically allocate memory for it (which doesn't make sense).
    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

Similar Threads

  1. Replies: 4
    Last Post: 05-06-2012, 06:24 AM
  2. Join variable with string
    By nicksnels in forum C Programming
    Replies: 2
    Last Post: 03-23-2010, 04:14 PM
  3. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  4. cin to array of chars
    By cdonlan in forum C++ Programming
    Replies: 1
    Last Post: 02-25-2005, 01:00 PM
  5. Array of pointers (chars)
    By Vber in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 04:29 AM