Re: Struggling with libraries
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Tue, 18 Apr 2006 01:05:55 GMT
websnarf@xxxxxxxxx writes:
Tony Burrows wrote:
I'm just learning C as another language, and I'm trying to build some
utilities into a library. I have this (crude I know) function:
void insert(char* insrt, char* source, int place){
char temp[strlen(insrt)+strlen(source)+1];
Your compiler lets you declare that? Is this some new C99 feature? I
think gcc lets you do things like this, but I don't think its ISO C89
compliant. Basically, declaration sizes have to be compile-time
constants, in most C compilers out there.
Yes, variable-length arrays are a new C99 feature, and gcc implements
something very similar.
So something like:
char * temp = (char *) malloc(1+strlen (instr)+strlen (source));
Thee cast is unnecessary and generally not recommended.
See <http://www.c-faq.com/>, questions 7.7, 7.7b, 7.7c.
(The cast is required in C++. A very few people have good reasons to
compile their code as both C and C++. A few others argue that the
cast is a good idea.)
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Struggling with libraries
- From: Tony Burrows
- Re: Struggling with libraries
- From: websnarf
- Struggling with libraries
- Prev by Date: gcc, aliasing rules and unions
- Next by Date: Re: Struggling with libraries
- Previous by thread: Re: Struggling with libraries
- Next by thread: Re: Struggling with libraries
- Index(es):
Relevant Pages
|