LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2006, 03:17 AM   #1
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Rep: Reputation: 15
strdup help


Hello,

I m writing a program, and after doing lots of research i found out that strdup can help....
i searched the man but i still need some example
can u provide with an example or a small program of how to use it ?
Thanks
 
Old 11-23-2006, 08:32 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
strdup makes a copy of a character string. Use it when your program needs to trash a string that you want to keep safe.
Code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int foo(char *keepme)
{
  char *working=strdup(keepme);
  if(working != NULL)
  {
     /* 
             code to work with or possibly trash working string goes here (strtok is an example
     */
     free(working);
  }
  else
  { 
      perror("Insufficient memory");
      exit(EXIT_FAILURE);  
  }
  return 1;
}
 
Old 11-23-2006, 08:34 AM   #3
studentlb
Member
 
Registered: Oct 2006
Posts: 53

Original Poster
Rep: Reputation: 15
thanks dude :d

Last edited by studentlb; 11-23-2006 at 08:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
help with strdup knobby67 Programming 2 10-22-2006 12:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration