#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Assign array column to string


    Hi,
    I need to search a string inside a char array diagonal.
    I found that with strstr I can search for a string inside another string.
    So I thought I could use a for to concatenate the coordenates of the array into some temp string, and then use strstr.
    Is that possible?
    I tried with:

    strcat(tempstring, array[i][j]) (inside some for's)

    but as usual, the first try didn't work (nor the second nor the third).
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    strcat() requires two strings. A C-style string is passed equivalent to a char pointer. array[i][j] is a single char. You're trying to use a char as a char pointer. The warning you got should have told you that.

    Construct the temp string by hand. Iterate through the array and tempstring assigning each individual char in the array to its corresponding position in tempstring. When you're done, assign a null-terminator (ie, '\0') to the position in tempstring immediately after the last char.

    I know of no function that appends a char on the end of a string, so we just need to assign that char to where the null-terminator is and then write a new null-terminator right after it.

    Similarly, to use strcat you would need to take that char and construct a string out of it by declaring a char array of size 2 and assign the char to [0] and '\0' to [1]. But if you're going to go through all that trouble, why not simply do that to tempstring?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by dwise1_aol
    strcat() requires two strings. A C-style string is passed equivalent to a char pointer. array[i][j] is a single char. You're trying to use a char as a char pointer. The warning you got should have told you that.

    Construct the temp string by hand. Iterate through the array and tempstring assigning each individual char in the array to its corresponding position in tempstring. When you're done, assign a null-terminator (ie, '\0') to the position in tempstring immediately after the last char.

    I know of no function that appends a char on the end of a string, so we just need to assign that char to where the null-terminator is and then write a new null-terminator right after it.

    Similarly, to use strcat you would need to take that char and construct a string out of it by declaring a char array of size 2 and assign the char to [0] and '\0' to [1]. But if you're going to go through all that trouble, why not simply do that to tempstring?
    It seems reasonable. I'll try that.
    Thanks for the input.

IMN logo majestic logo threadwatch logo seochat tools logo