Thread: what's wrong with this piece of code?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    Unhappy what's wrong with this piece of code?

    below code can be compiled but can not run, what's wrong with it?

    Code:
    #include <stdio.h>
    void getmemory(char ** pchar) {
    	pchar = (char **)malloc(100);
    }
    int main(int argc, char *argv[]) {
    	char *pstr = NULL;
    	getmemory(&pstr);
    	strcpy(pstr, "hello world");
    	printf(pstr);
            return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    	pchar = (char **)malloc(100);
                // What you mean is 
    
                 *pchar = malloc( 100 );
    It's better to use printf("%s",pstr).

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    yes , i think so ,
    thanks , it's okay now.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Bayint Naung View Post
    It's better to use printf("%s",pstr).
    Well, it's true that it's a security risk if you don't use that when trying to print user input.
    But if you are in control of what you print, then it's OK. Still, better safe than sorry.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-20-2007, 01:07 PM
  2. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  3. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM

Tags for this Thread