Chapter Contents

Previous

Next
strsave

strsave



Allocate a Copy of a Character String

Portability: SAS/C extension


SYNOPSIS
DESCRIPTION
RETURN VALUE
ERRORS
CAUTION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <lcstring.h>

char *strsave(const char *str);


DESCRIPTION

strsave allocates a private copy of a character string (using the malloc function).


RETURN VALUE

strsave returns the address of the copy of the string, or NULL if no memory is available for a copy.


ERRORS

User ABEND 1205 and 1206 may occur if memory management data areas have been overlaid.


CAUTION

A protection or addressing exception may occur if the argument string is not properly terminated.

The copy should be released by a call to free when it is no longer required.


EXAMPLE

#include <lcstring.h>
#include <stdio.h>

static char *filename;
static int couldopen(void);

main()
{
   if (couldopen() == 0)
      printf("The file "%s" was opened successfully.\n",
             filename);
   else
      printf("The file "%s" was not opened successfully.\n",
             filename);
   return;
}

int couldopen(void)
{
   char buf[FILENAME_MAX];
   FILE *f;
   puts("Enter a file name:");
   gets(buf);
   filename = strsave(buf);  /* Save the file name. Because */
                             /* buf is auto, saving the     */
                             /* address of buf is not safe. */
   f = fopen(buf, "r");
   if (f){
      fclose(f);
      return 0;              /* success                     */
   }
   else return -1;           /* failure                     */
}


RELATED FUNCTIONS

strcpy


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.