//Program in c to learn malloc function, mallocinc2.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char name[10];
printf("Enter a Name:");
scanf("%s",name);
printf("\n You entered: %s \n", name);
char *p;
p=(char *)malloc(sizeof(strlen(name)-1));
strcpy(p,name);
printf("Copied string is: %s \n",p);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
char name[10];
printf("Enter a Name:");
scanf("%s",name);
printf("\n You entered: %s \n", name);
char *p;
p=(char *)malloc(sizeof(strlen(name)-1));
strcpy(p,name);
printf("Copied string is: %s \n",p);
return 0;
}
How to use malloc function in C
ReplyDeleteHow to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
How to use malloc function in C
//Program in c++ to learn new
ReplyDelete//how to use new in C++
#include
int main()
{
char name[10]="xyz";
cout<<("\n You entered: \n", name);
char *p;
p=new char[strlen(name)]- 1;// Note the Sysntax
strcpy(p,name);
cout<<("\n copied string is: \n", name);
return 0;
}
//C++ program to use new
ReplyDelete//How to use new in C++
int main()
{
// allocate memory for the string
//char *string1 = (char *) malloc(100*sizeof(char));
char *string1;
string1=new char;
// write some data to the string
strcpy(string1, "Hello String world xyz abcdef zxy!");
// display the string
//printf("%s\n", string1);
cout<<"String is:"<<string1<<endl;
// remove the string from memory
//delete string1;
return 0;
}