0

I'm currently trying to make a program in c which will return a pointer to an array of 2 strings. The first is the characters of the string s that are in the odd position and the second are the characters in the even position. I'm not experienced in C so I need a bit of help with this program. I've been trying to code using what I know from python and java but it doesn't seem to follow the same principles with pointers. Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char **parity_strings(const char *s){

char dest[malloc((char)sizeof(s)/2 + 1)][malloc((char)sizeof(s)/2 + 1)]; //trying to allocate memory to an array of size 2 which will hold 2 strings.

int i;
for(i = 0; i < sizeof(s); i+= 2){    //iterating through odd strings
    s[0] += dest[i];
}
for(i= 2; i< sizeof(s); i += 2){    //iterating through even strings (I suppose i could have just appended using 1 for loop but oh well
    s[1] += dest[i];
}

return dest;


}

int main(int argc, char **argv) {
char **r = parity_strings(argv[1]);
printf("%s %s %s\n", r[0], r[1], argv[1]);
return 0;
} 

memory allocation is just a pain too...I have no clue if it's doing what I intend on it doing. I'm trying to allocate the size of the string in bytes + 1 byte into each index of the array Dest.

any ideas on how to fix this? Thanks.

user125535
  • 240
  • 4
  • 15

2 Answers2

2

This line will not do anything good:

char dest[malloc((char)sizeof(s)/2 + 1)][malloc((char)sizeof(s)/2 + 1)];

malloc returns a pointer to the newly allocated memory. In your line above, the square brackets in dest[][] need unsigned integers. Pointers can be casted to integers, but that isn’t what you want there at all. It might compile, but it probably won’t run, and certainly won’t do what you want.

Also, sizeof(s) returns the size of the pointer to s, not the length of the string. Strings in C are really just null-terminated arrays of chars, and arrays are passed to functions with a pointer, not their entire contents. To get the length of a string, use strlen(s) instead.

You could do something like this:

char *destodd = malloc((strlen(s)/2 + 2));
char *desteven = malloc((strlen(s)/2 + 2));
char **dest = malloc(sizeof(char *) * 2);
dest[0] = desteven;
dest[1] = destodd;

I changed your + 1 above to +2. A string of length 3 needs 3 characters in destodd: one for character 1, one for character 3, and one for the NUL terminator.

It’s tricky to malloc a multi-dimensional array in C. A one-dimensional array, on the other hand, is easy. Just treat destodd and desteven like they’re arrays, even though they’re really pointers:

for (i = 0; i < strlen(s); i += 2){
    desteven[i] = 'a'; // Fix this
    destodd[i] = 'b';
}

The code in your for loops didn’t look like it would work. It looks like you may have been trying to use += to concatenate strings, but it only does addition of numbers. I couldn’t quickly figure out what you should set in the for loop, so 'a' and 'b' are just placeholders.

Community
  • 1
  • 1
yellowantphil
  • 1,483
  • 5
  • 21
  • 30
1

You have a few issues. As your compiler should tell you, char dest[malloc()] requires a pointer-to-unsigned cast, which is legal but is not what you want. More importantly, returning a pointer to an array allocated on the stack results in undefined behavior if you dereference the pointer, because the compiler may have already deallocated the memory. I'm not exactly sure what the intended output of the function is, but in terms of filling two char arrays, in my opinion the easiest way to do it is this:

char **parity_strings(char* buf) //Please avoid single letter variable names for anything but loop control
{
    size_t buflen = strlen(buf);
    if (NULL == char** dest = malloc(2 * sizeof(*dest)))
        ;//handle memory allocation error
    if (NULL == dest[0] = malloc(buflen * sizeof(*buf)))
        ;//handle memory allocation error
    if (NULL == dest[1] = malloc(buflen * sizeof(*buf)))
        ;//handle memory allocation error
    //Note that you would do the above two lines in a loop for a variable sized multidimensional array
    strncpy(dest[0], buf, 500);
    strncpy(dest[1], buf, 500); //If you need strings larger than 500 change as necessary, mostly only needed if you are taking input from someone else but it's good practice to use strncpy over strcpy)
    return dest;
}
IllusiveBrian
  • 3,105
  • 2
  • 14
  • 17