Re: Struggling with libraries



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.
.



Relevant Pages

  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)
  • Re: Properties
    ... Is there any reason why this would not work and not simplify the ... Why should I have to declare any variable most of the time? ... The reason you explicitly declare fields used by a property is that the ... compiler needs to know what the code in the property does. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: use of erf intrinsic
    ... while it is also legal to declare a referenced function ... within the calling functionor even block, a la classic FORTRAN, ... The implementation (compiler) is required to check that references ... If you use K&R1 syntax, the compiler is not required to detect ...
    (comp.lang.fortran)
  • Re: Primitive curiosity...
    ... those types after the compiler has already used the information. ... "dispatch table" or something like that. ... contains the addresses of the primitive subprograms of the type. ... When you declare a type extension, ...
    (comp.lang.ada)
  • Re: using iso_c_binding
    ... start with the ones the compiler noticed for you. ... I prefer to declare the type of a function in a separate type ... There are some funny special-case rules to ... Another one that g95 bitched about after I fixed all of the above was ...
    (comp.lang.fortran)