It's UWAweek 8


This forum is provided to promote discussion amongst students enrolled in CITS2002 Systems Programming.
Please consider offering answers and suggestions to help other students! And if you fix a problem by following a suggestion here, it would be great if other interested students could see a short "Great, fixed it!"  followup message.
Displaying selected article
Showing 1 of 1168 articles.
Currently 1 other person reading this forum.

 UWA week 39 (2nd semester, week 9) ↓
SVG not supported

Login to reply

3:36pm Wed 27th Sep, Christopher M.

> In the recent lab I was attempting to use strdup() and compiling with the normal compilation command and the compiler was not recognising strdup() as a valid standard C function. I believe we are required to use it in this next project, how are we meant to run our code using that? I believe it works on Macs but WSL on Windows for me doesn't.
The function strdup() has never been defined in a C standard, until its appearance in the new C23 standard (though why release it on 1st April!?) But strdup() has long been defined in earlier POSIX, XOPEN, and GNU standards, and if we state which standards we want our programs to conform to, then we can access the strdup() declaration from the <string.h> header file. The 'correct' way to do this is to define the required standard before #include-ing the header file. For example, if we wish to follow the POSIX standard released September 2008, we could use:
#define _POSIX_C_SOURCE  200809L

#include <string.h>
A less correct (less pedantic) way to declare strdup() is to just provide the declaration given in man entry for strdup() :
extern char *strdup(const char *str);
All of the above applies if using Linux; Apple 'holds your hand' by silently supporting the 2001 POSIX standard (and beyond), so strdup() is always available.

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Written by [email protected]
Powered by history
Feedback always welcome - it makes our software better!
Last modified  5:07AM Sep 06 2023
Privacy policy