tryllz
tryllz

Reputation: 33

Dev C++ Crashes on this code

I have the below code and I have 2 problems, one being that I an error of [Warning] initialization makes pointer from integer without a cast even, I am not able to understand why, the warning shows on the line : printf("String 1 : %s and Its length is %i \nString 2 : %s and its length is %i\n", strg1, strlen(strg1), strg2, strlen(strg2));

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

int strg_len2(char *s);

int main(void)
{
   /*  Declare variables.  */
   char strg1[] = "This is one string ";
   char strg2[] = "and this is a second string";
   char *strg3 = /* allocate memory dynamically */

   /*  Print the strg1, strg2 and their lengths.  */
//   printf("String 1 : %s\nString 2 : %s\n", strg1, strg2);
   printf("String 1 : %s and Its length is %i \nString 2 : %s and its length is %i\n", strg1, strlen(strg1), strg2, strlen(strg2));

   /* Allocate memory for strg3 */
    strg3 = (char*) malloc((strlen(strg1)+strlen(strg2)*sizeof(char)));

   /* Combine the strings (strg1 and strg2) to strg3*/
   strcpy(strg3,strg1);
   strcat(strg3,strg2);

   /*print strg3 and its length */
   printf("%s, now length of string 3 is : %i\n", strg3, strlen(strg3));

   /* strncat() - add only 10 char of strg2 to strg1 and store them into strg3 */
    printf("\nContatanation of String 1 And 10 characters of string 2 : %s", strncat(strg1, strg2,10));
    strg3 = strncat(strg1, strg2,10);
   /*print strg3 and its length */
    printf("%s\t%i", strg3, strlen(strg3));

} /*end main()*/

The second error is when I have combined 2 strings into the third one, if I print the 3rd is string there is error as in the image.enter image description here

This part : printf("%s, now length of string 3 is : %i\n", strg3, strlen(strg3));

Upvotes: 0

Views: 87

Answers (2)

John D
John D

Reputation: 1637

You're missing a semicolon after char *strg3 =. The compiler ignores the comments and looks for what you're trying to assign to strg3. It finds the printf call and assigns the result of that. The printf returns an int (which is the number of characters printed). So compiler thinks you are assigning an int to char* strg3. Just declare it without assigning anything eg char *strg3;

You need to allocate one more char to strg3 to hold the 2 other strings.

strg3 = (char*) malloc( (strlen(strg1)+strlen(strg2) + 1) *sizeof(char));

C-style strings need space for an extra \0 at the end. The strcat will add the \0 but that will then write over memory that does not belong to strg3 with possibly disastrous results like writing over the return address of the function.

Upvotes: 0

Ton Plooij
Ton Plooij

Reputation: 2641

Since the strg3 declaration is not closed with a semicolon, it says:

char *strg3 = printf("String 1 : %s and Its length is %i \nString 2 : %s and its length is %i\n", strg1, strlen(strg1), strg2, strlen(strg2));

Second issue is that the strg3 allocation is one byte (or actually one size(char)) short:

strg3 = (char*) malloc((strlen(strg1)+strlen(strg2)*sizeof(char)));

This does not include space for the '\0' terminator. The following strcat will overwrite the allocated memory buffer.

Upvotes: 1

Related Questions