0

this is part of a homework assignment using structs and I can't seem to understand this one function. The function is string_t *concat (string_t *s1, string_t *s2) and it returns the new string struct. This is what I have so far, and it crashes the compiler whenever it's reached. The program compiles but, "file".exe has stopped working error comes up when executing. Any help would be greatly appreciated. Thanks!

typedef struct string{ //String struct (in .h file)

char *line;
int length;

} string_t;


string_t* concat(string_t *s1, string_t *s2) { //actual function (in .c)

int len1, len2;
len1 = length(s1);
len2 = length(s2);

int i, j, s;

string_t *newStr;
newStr = (string_t*)malloc(sizeof(string_t)*2);


for (i = 0; i<len1; i++) {
    *((newStr->line)+i) = *((s1->line)+i);
    }

for (j=0; j<len2; j++) {
    *((newStr->line)+(i+j)) = *((s2->line)+j);
    }

*((newStr->line)+(i+j))='\0';

return newStr;

}



concat(s1, s2); //tests function
apkim221
  • 121
  • 2
  • 4
  • 11

2 Answers2

4
newStr = (string_t*)malloc(sizeof(string_t)*2);

You allocate memory for newStr but you don't allocate memory for newStr->line. Try something like:

newStr = malloc(sizeof *newStr);
newStr->line = malloc(s1->length + s2->length + 1);

Side note: *((newStr->line)+i) can be written as newStr->line[i].

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Yeah, I know about the second part, but our professor does not allow us to use indices, just pointer arithmetic – apkim221 Feb 27 '13 at 04:18
  • 1
    @user2041197 :( Who pays these people ? – cnicutar Feb 27 '13 at 04:18
  • I did what you said, but when I try to test it I do s3 = concat(s1,s2) where s3 is a string_t* and I get an error saying assignment makes pointer from integer without a cast – apkim221 Feb 27 '13 at 04:23
  • 1
    Likewise, you can lose the `strlen()` invokes (assuming you've been wise in ensuring your `.length` member is accurate). – WhozCraig Feb 27 '13 at 04:23
  • I second that, I hadn't noticed there was a `length` field. – cnicutar Feb 27 '13 at 04:23
  • Heh. fix that and save me the hassle of writing an answer =P (I'll upvote regardless). – WhozCraig Feb 27 '13 at 04:26
  • @WhozCraig I'm not after the rep, I've enough. But I did edit since it was the right and obvious thing :-) – cnicutar Feb 27 '13 at 04:28
  • Oh I know, I wasn't up-voting to toss the rep (its somewhat pointless by now for you). But it does help make the answer stand out as being applicable. – WhozCraig Feb 27 '13 at 04:29
  • Please consider [not casting the return value of `malloc()`, in C](http://stackoverflow.com/a/605858/28169). Thanks. – unwind Feb 27 '13 at 06:50
0

BTW, here's a way to cat without that ugly ptr math syntax:

char* dest = newStr->line;

const char* src = s1->line;
while (*src)
{
  *dest = *src;
  ++dest;
  ++src;
}

src = s2->line;
while (*src)
{
  *dest = *src;
  ++dest;
  ++src;
}

*dest = '\0';
Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
  • @modifiablelvalue, I do not like the post increment operators in C because there is a temporary that is created. Most of the time it is more clear and obvious (IMHO) to use the preincrement operator almost exclusively. If you like having to human-parse looking for all the possible craziness that the programmer can pack into one line, then there is absolutely nothing wrong with it. – Josh Petitt Feb 27 '13 at 04:45
  • @modifiablelvalue, and I also agree it does work, I just wouldn't use it in my own code that I write. – Josh Petitt Feb 27 '13 at 04:47
  • @modifiablelvalue, also that wouldn't quite work for the first loop because it would insert a '\0'. It would work for the second loop though. – Josh Petitt Feb 27 '13 at 04:50
  • For all. of your criticism, at least my loop doesn't invoke undefined behaviour. Just by looking at yours I can tell you didn't bother testing it. – autistic Feb 27 '13 at 14:55
  • @modifiablelvalue, guilty as charged, thank you for you help. I've corrected and tested the lines above. – Josh Petitt Feb 27 '13 at 16:10
  • @modifiablelvalue, also I didn't think I was criticizing? I thought I was answering your "What's wrong with" comment (BTW, I stated a number of times that nothing was wrong)? I'm sorry if I've offended you. – Josh Petitt Feb 27 '13 at 16:11