C data structure library [closed] - c

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I want to use a stack in C, anybody recommend a library?
For example for a hash table I used UThash.
Thanks!

Stack implementation fits in single sheet of paper.
That's simplest stack example
int stack[1000];
int *sp;
#define push(sp, n) (*((sp)++) = (n))
#define pop(sp) (*--(sp))
...
{
sp = stack; /* initialize */
push(sp, 10);
x = pop(sp);
}

Here is a similar question:
Are there any open source C libraries with common data structures?
And here is CCAN, C's equivalent to CPAN:
http://ccan.ozlabs.org/

If you can fudge it a bit and use C++, Qt is a really great library with a lot of basic data structures.

Related

If statement and sequential execution [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I have a very short (and pretty much noob) question. I have a function that includes:
void function(int x)
{
x = 3;
if (x == 4)
printf("HI!");
x = 4;
}
Will the word HI! be printed? In other words, is a C program read sequentially or not?
Thanks a lot!
No, the code compiles to a sequence of instructions which happen sequentially one after the other. The comparison to 4 will always before the assignment x = 4. So it will be false.
This type of order is guaranteed when you are dealing with a single thread. When you have multiple threads you can get strange results and race-conditions unless you are careful.
It will not be printed. The line above is what matters of course.

C/C++: Is there a specific reason why "void" was not simply defined as "typedef struct{} void" (i.e. an empty struct) with appropriate casting rules? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
As an aside: Such a standard typedef would have help reduce the number of reserved words in C/C++, and relegated void to a simple type declaration, e.g. in <stddef.h>. (c.f. SRB in AB33/Mar 1972 - PDF=7kB)
That's pretty speculative. But one good reason is because void is not an empty value; it's the absence of a value. For instance, a function with a signature of int f(void) takes zero arguments, not one.

How one did manage to create and access structures that behaved like arrays in C89? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Before C99 programmers were obliged to define all arrays with fixed sizes that were known at compile time. How did people manage to create and access structures that behaved like arrays but whose sizes were not known until runtime?
With the use of malloc(3) and free(3) to do dynamic memory management. It's still done today.
void f(int n)
{
int *nInts = malloc(n * sizeof(int));
/* do stuff with 'nInts' */
free(nInts);
}
I think you are referring to this.
As said there, it's not really sure if it's legal or portable. That said I have seen code like this but never written it myself.

Why is FILE all-caps as in FILE*? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
This just seems odd to me, most other things are lower case.
Is there some historical reason?
It's a macro. Macros have historically had all caps in C. Unfortunately recent trends appear to have broken that fact.
Small history lesson: Also, FILE was an io buffer abstraction in UNIX v7 libc stdio. A FILE doesn't necessarily represent a physical file, just something that can do block IO. Source:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/stdio.h.html
"file" was already defined by the kernel as well:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/sys/file.h.html
As someone else said here it's probably a typedef now, but I don't think C had typedefs back in '79 as it only just had structs. Then again I wasn't born then so... :)
It's all caps almost certainly because at least originally it was a macro. Nowadays, chances are pretty good that it's a typedef instead, but nobody changed the name to boot (and doing so would be a pretty lousy idea).

Does C require that variables are declared at start of scope? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
The C language requires that all variables used in the program should be declared before any other statements.
Why was this of significance? Was it for readability? If it was for readability, then why don't other languages follow the same rule?
I personally prefer declaring all my variables at the very start.
Moderators: Please delete if this is off-topic or already been asked. I was just keen to know improve my knowledge. Thanks.
Like other people said in comments now it's not mandatory anymore. Anyway it wasn't for readability. C is a very old language and one of its peculiar features is that a C file can be compiled with just one parsing. I think that the earliest version of the language made the choice to declare variable at the beginning for simplify the parsing

Resources