Dynamic Memory Allocation 1- Revisit the matrix addition function. Write the pro
ID: 3764203 • Letter: D
Question
Dynamic Memory Allocation
1- Revisit the matrix addition function. Write the program so that it would not need to take a third pointer for the result matrix. Instead, have the function return a pointer to the results matrix. Provide space for the results matrix inside of the function through dynamic memory allocation.
2- Write a string concatenation function that takes two pointers to the strings to be concatenated and have the function return a pointer to the concatenated string. Again, provide the space for the result string through dynamic memory allocation.
Please i need the code in C Program not C++ or C+!
Explanation / Answer
2.
Program code:
// stringconcat.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
char *stringConcat(const char *, char *);
//main function
int main()
{
//declare the required variables
int i=0;
char *str1="good",*str2="morning";
printf("String after concatenation of string: %s and string: %s = "%s" ", str1,str2,stringConcat(str1,str2));
system("pause");
return 0;
}
//definition of function to cocatenate the two strings
char *stringConcat(const char *s, char *d)
{
char *str3;
str3 = (char *)malloc(sizeof(char)*(strlen(d)+strlen(d)+1));
if(str3!=NULL)
{
strcpy(str3,s);
strcat(str3," ");
strcat(str3,d);
}
return str3;
}
Sample output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.