-2

helloeveryone. I am fairly new to programming and currently trying to learn C programming to advance further in any of my projects. I've just learned how to use malloc and realloc, and all seemed good until I attempted to use strcat to combine two given strings from multidimensional array.

I am supposed to get combination of two strings based on the user inputs, and strangely, the first character is either missing or replaced by other characters... I'll include the source code as well as the output below. I'd really appreciate you help. Thanks in advance!! ( don't mind the Korean at the end... I am korean :P)

enter code here
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int main(void)
{
int i, j;
const int row = 3;
char *pstr[3];
char temp[100];
int k,p = 0;




printf("Type in any three characters\n");
for (i = 0; i < row; i++)
{
    pstr[i] = (char *)malloc(strlen(temp) + 1); //initialize the lenght of the elements in 2 part of 2D array of char b[ROW] via length of the given string
}
for (i = 0; i < row; i++)
{
    scanf("%s", temp);
    strcpy(pstr[i], temp);
}
printf("\n");

for (i = 0; i < row; i++)
{
    printf("%s\n", pstr[i]);
}

    scanf("%d", &p);
    scanf("%d", &k);

printf("%s\n", pstr[p]);
printf("%s\n", pstr[k]);

*pstr[k] = (char *)realloc(pstr[k], strlen(pstr[p])+100);

strcat(pstr[k], pstr[p]);
printf("%s", pstr[k]);


for (i = 0; i < row; i++)
{
    free(pstr[i]);
}

return 0;
}

\ output::LINK IS AN INTERNATIONAL SIGN FOR , IMAGE OVER HERE!!!

Jay Choi
  • 1
  • 1
  • There is no multidimensional array in your code! A pointer is not an array. – too honest for this site Jun 12 '17 at 11:38
  • And don't post images of text! – too honest for this site Jun 12 '17 at 11:39
  • @tilz0R: Why don't you just provide the link? But you might have a new, good reson why the cast is justified. – too honest for this site Jun 12 '17 at 11:40
  • @tilz0R: In case you missed the news: C and C++ are distinct languages anyway. Any non-trivial code is not portable! For instance: try `int i = 10, a[i];` in a block. And others. Whoever told you different should leave the 1980ies/early 90ies. – too honest for this site Jun 12 '17 at 11:46
  • This is true. But as far I know (and you know this too), then you can write C code which can later be compiled in C++ environment. If you don't cast malloc, you will have error. This is not really *non-trivial* code. So, include stdlib and cast malloc, it will work always. Nice ;) – unalignedmemoryaccess Jun 12 '17 at 11:47
  • @tilz0R: If you restrict your code to the common subset **and** constructs only which have the **identical semantics**, you can hardly write any useful code. For instance you can't write `const` correct code. Either way, it is not really C code. And why if you write C code would you want to compile with a C++ compiler anyway? Compile C as C; linking with C++ is not a problem at all. Anything else is plain nonsense! Anything else is bad practice. But if you only know a hammer, every problem looks like a nail … – too honest for this site Jun 12 '17 at 11:53

2 Answers2

1

Two major problems:

  1. You use temp before it have been initialized, when its contents is indeterminate and that will lead to undefined behavior.

  2. When you do *pstr[k] = realloc(...) you dereference the pointer in pstr[k] and gets it's first element, which is a single character. You then assign the result of the realloc call to this char element. So you basically lose the actual pointer and pstr[k] will still point to the same memory (which might now be invalid).

There are other problems, but these two are the worst.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for the answer. I changed the code as you've said, and I've also changed the strlen since I seemed to get errors. But I still wants to use strlen. Are there ways to do that? – Jay Choi Jun 12 '17 at 12:10
  • Oh... nv mind. I just fixed it thanks. I guess it was a pretty stupid question now I look back – Jay Choi Jun 12 '17 at 12:14
0

I found these in your code

1) if k or p is greater than 2 it will give runtime error

2) *pstr[k] = (char *)realloc(pstr[k], strlen(pstr[p])+100);

but this line can give error in compile time also(mac at least) - as they are not same so you may change like this -

*pstr[k] = *(char *)realloc(pstr[k], strlen(pstr[p])+100);

3) After realloc you will get exception in free. see this - How free memory after of realloc

Deb S
  • 509
  • 5
  • 16