20120113

How to use malloc in C

//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;
}

Previous                             Home                               Next

3 comments:

  1. 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
    How to use malloc function in C
    How to use malloc function in C

    ReplyDelete
  2. //Program in c++ to learn new
    //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;
    }

    ReplyDelete
  3. //C++ program to use new
    //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;
    }

    ReplyDelete