https://software.codidact.com/categories/38/tags/3296.rssNew Posts Tagged 'c' - Software DevelopmentSoftware Development - Codidact2024-03-28T14:30:33Zhttps://software.codidact.com/posts/291195Pattern / architecture for interfacing with components in Cpatriko8520https://software.codidact.com/users/804682024-03-28T09:51:15Z2024-03-28T14:30:33Z<p>I'm working on the architecture, where the assumption is to easily extend the options in the system and also to provide some kind of encapsulation (from the main we could only access the type1 /...https://software.codidact.com/posts/281518Is strcpy dangerous and what should be used instead?Lundinhttps://software.codidact.com/users/81762021-04-27T13:17:25Z2024-03-22T11:09:31Z<p>I heard rumours that the <code>strcpy</code> function is dangerous and shouldn't be used. Supposedly it can be exploited to create buffer overflows somehow.</p>
<p>And indeed when I compile my ...https://software.codidact.com/posts/291111How can I manage multiple consecutive strings in a buffer (and add more later)?Karl Knechtelhttps://software.codidact.com/users/646562024-03-21T19:15:40Z2024-03-22T10:15:47Z<p><em>This question is inspired by <a href="https://stackoverflow.com/questions/78202169">If I have a char array containing strings with a null byte (\0) terminating each string, how would I add a...https://software.codidact.com/posts/290971A small header-only input output libraryMelkor-1https://software.codidact.com/users/783832024-03-01T09:31:53Z2024-03-09T08:53:58Z<p>The library (inspired by <a href="https://github.com/nothings/stb?tab=readme-ov-file">stb libraries</a>) attempts to provide some commonly used functions (reading a file into memory, determining...https://software.codidact.com/posts/290881Are there technical reasons to pick one struct coding style over the other?Lundinhttps://software.codidact.com/users/81762024-02-20T14:11:26Z2024-02-21T15:53:27Z<p>C offers two different styles when it comes to structs (and union/enum too). Either declare them using a struct tag only ("struct tag style"):</p>
<pre><code>struct my_type
{ ... };
struct...https://software.codidact.com/posts/290857Trie Implementation, Graph Visualization and Auto-Completion in CMelkor-1https://software.codidact.com/users/783832024-02-18T09:10:06Z2024-02-20T10:30:07Z<p>Given a list of strings (say a text file containing some C symbols: <a href="https://github.com/Melkor-1/Trie/blob/main/c-symbols.txt">c-symbols.txt</a>), the program can:</p>
<ol>
<li>Generat...https://software.codidact.com/posts/290314What is the purpose of having underscored names and then defining a non-underscored alias to it?90 98https://software.codidact.com/users/690602023-11-29T04:49:20Z2023-11-29T19:45:56Z<p>In a C implementation in <code><stdio.h></code> on Linux I saw something like:</p>
<pre><code>extern FILE *__stdinp;
extern FILE *__stdoutp;
extern FILE *__stderrp;
</code></pre>
<p>A...https://software.codidact.com/posts/279576What is do { } while(0) in macros and should we use it?Lundinhttps://software.codidact.com/users/81762020-11-26T07:57:05Z2023-11-21T14:31:42Z<p><strong>Background</strong></p>
<p>I can see the need to use <code>{ }</code> when implementing a function-like macro such as this one:</p>
<pre><code>#define HCF(code) fprintf(stderr, "halt a...https://software.codidact.com/posts/290205When should I parenthesize macro arguments in C?alxhttps://software.codidact.com/users/539372023-11-10T23:47:04Z2023-11-13T14:39:59Z<p>I've seen macros use parentheses to enclose its arguments. Most of these make sense, as in</p>
<pre><code class="language-c">#define sum(a, b) ((a) + (b))
</code></pre>
<p>The outer prevent...https://software.codidact.com/posts/289828What is the meaning of "short circuit" operators?Lundinhttps://software.codidact.com/users/81762023-09-28T08:06:41Z2023-09-28T19:46:29Z<p>When reading about various operators used by programming languages, the term "short circuit behavior" is often used. For example in this C code:</p>
<pre><code>int a = 0;
a && b++
</c...https://software.codidact.com/posts/283440How to declare variable-length arrays correctly?Lundinhttps://software.codidact.com/users/81762021-08-16T08:49:38Z2023-08-25T12:55:11Z<p><em>This is meant as a FAQ Q&A regarding variable-length arrays (VLA). The two bugs described below are surprisingly common.</em></p>
<hr>
<p>I'm trying to use the variable-length arrays f...https://software.codidact.com/posts/289488Why does calloc accept 2 arguments, and with what arguments should one call it?Lover of Structurehttps://software.codidact.com/users/640472023-08-21T18:11:43Z2023-08-22T10:04:44Z<p>According to the standard (C17 draft, 7.22.3.2) The function <code>calloc</code></p>
<blockquote>
<p><code>void *calloc(size_t nmemb, size_t size);</code></p>
</blockquote>
<p>"allocates spa...https://software.codidact.com/posts/289414What is C23 and why should I care?Lundinhttps://software.codidact.com/users/81762023-08-17T08:17:49Z2023-08-18T07:33:34Z<p>The C language has gone through many iterations and the latest one not yet released is informally called "C23", supposedly because they hoped to release it in 2023. The latest draft version <a h...https://software.codidact.com/posts/285898How to properly use malloc?klutthttps://software.codidact.com/users/81962022-02-10T01:11:06Z2023-08-10T10:59:15Z<p>I have seen this construct quite a lot:</p>
<pre><code>int *p = (int*) malloc(n * sizeof(int));
</code></pre>
<p>Is this how it should be? What is the proper way to use <code>malloc</code>? I...https://software.codidact.com/posts/288916What are disadvantages of static functions (ie functions with internal linkage) in C?Lover of Structurehttps://software.codidact.com/users/640472023-07-08T13:03:51Z2023-07-10T06:47:46Z<p>Functions in C have external linkage by default. In other words, the storage class specifier <code>extern</code> is applied to functions by default, with the effect that they are visible to all ...https://software.codidact.com/posts/282485constructor in Cdeleted user#2021-07-01T02:53:08Z2023-07-07T16:37:29Z<pre><code class="language-c">#include<stdio.h>
struct Book {
char title[20];
char author[20];
int pages;
};
void init_Book_types(struct Book* aTitle,struct Book* aAuthor,struct ...https://software.codidact.com/posts/285715Are there references in C?Lundinhttps://software.codidact.com/users/81762022-01-27T14:11:56Z2023-07-01T06:16:38Z<p>When reading posts at programming sites such as this one, I frequently encounter people saying things like:</p>
<p><em><strong>"There is no pass-by-reference in C, everything is passed by value...https://software.codidact.com/posts/288138Storing more bytes than a union member has, but less than the union size, with memcpy(3)alxhttps://software.codidact.com/users/539372023-05-21T15:16:51Z2023-06-09T14:29:28Z<p>Let's say we have an object, we store it in a union (into some other narrower type, but with memcpy(3), so it's allowed --I guess--), and then read it from the union via it's original type (so n...https://software.codidact.com/posts/285050What does the static keyword do in C?Lundinhttps://software.codidact.com/users/81762021-12-02T09:51:53Z2023-05-24T09:46:55Z<p>What exactly does the <code>static</code> keyword do in C? I have seen it used in several diverse contexts:</p>
<p><strong>1) As a variable outside a function:</strong></p>
<pre><code class="l...https://software.codidact.com/posts/288017Can freed pointers undergo lvalue conversion?alxhttps://software.codidact.com/users/539372023-04-02T15:29:06Z2023-04-13T19:14:30Z<pre><code>char *p, *q;
p = malloc(1);
free(p);
q = p; // lvalue conversion
</code></pre>
<p>Is the last lvalue conversion (<code>= p;</code>) Undefined Behavior or not?
We didn't take t...https://software.codidact.com/posts/287905memcmp(3) memory containing invalid valuesalxhttps://software.codidact.com/users/539372023-02-17T20:46:54Z2023-02-21T15:17:05Z<p>What does it mean that we can use memcmp(3) on invalid values?</p>
<p>ISO C allows comparing an invalid value through memcmp(3), because it doesn't read the value, but rather its representation...https://software.codidact.com/posts/287754Is partial allocation of an object Undefined Behavior?alxhttps://software.codidact.com/users/539372023-01-21T03:49:28Z2023-01-23T11:00:49Z<p>Is it valid to partly allocate an object, as long as you only use the allocated part of it?</p>
<pre><code class="language-c">#include <stdio.h>
#include <stdlib.h>
struct s {
...https://software.codidact.com/posts/287748Strict aliasing rules and function boundariesalxhttps://software.codidact.com/users/539372023-01-20T00:02:49Z2023-01-20T07:40:22Z<p>Let's analyze this code, assuming an architecture where the alignment of <code>int64_t</code> is the same as that of <code>double</code>:</p>
<pre><code class="language-c">void
bar(double *f, ...https://software.codidact.com/posts/285946stpecpy(): Design a better string copy function that truncatesalxhttps://software.codidact.com/users/539372022-02-12T23:52:35Z2022-12-10T22:14:28Z<p>I was directed a few days ago to a <a href="https://www.symas.com/post/the-sad-state-of-c-strings">post</a> about a string copy function, which IMO improves the commonly known string copy functi...https://software.codidact.com/posts/286575How to write a macro that discards the const qualifier, for any type?alxhttps://software.codidact.com/users/539372022-06-10T14:55:22Z2022-11-02T23:03:17Z<p>How to write a macro that discards the const qualifier, for any type?</p>
<p>I hope some combination of <code>typeof</code> and a cast will do, but haven't found the combination.</p>
<p>I trie...https://software.codidact.com/posts/287057C naming convention, module trigrams?AdriZhttps://software.codidact.com/users/569932022-09-23T12:36:26Z2022-09-26T21:57:38Z<p>For my company, I'm writing naming conventions for embedded code in C language.</p>
<ul>
<li>Function names must be named in lowerCamelCase() and start with a
verb.</li>
<li>Global variables...https://software.codidact.com/posts/286974Cast uninitialized variable to (void)Estelahttps://software.codidact.com/users/533202022-09-09T18:11:41Z2022-09-15T16:38:42Z<p>Is it undefined behaviour to cast an uninitialized variable to (void)?</p>
<p>Example:</p>
<pre><code>int main()
{
int x;
(void)x;
return 0;
}
</code></pre>
<!-- g: js, mdit -->https://software.codidact.com/posts/283888How to do private encapsulation in C?Lundinhttps://software.codidact.com/users/81762021-09-01T09:27:21Z2022-08-19T14:19:52Z<p>I'm using an object-oriented design for my C project and trying to implement classes with private encapsulation. How do I do this? Some things I've tried that are problematic:</p>
<ul>
<li>
<...https://software.codidact.com/posts/285218How to efficiently remove every occurrence of one string inside anotherdumplingshttps://software.codidact.com/users/547102021-12-13T08:15:14Z2022-07-13T07:55:03Z<p>I have two strings and want to remove every occurrence of the second string inside the first one starting from the beginning of the first string and as if each occurrence was removed immediately...https://software.codidact.com/posts/286627Assert that some code is not present in the final binary, at compile or link time.alxhttps://software.codidact.com/users/539372022-06-23T12:45:14Z2022-06-27T09:24:11Z<p>I'd like to assert that some code can be optimized out, and is not present in the final binary object.</p>
<pre><code class="language-c">#define CONSTANT 0
#if (!CONSTANT)
[[landmine_A]]
...https://software.codidact.com/posts/285148Regarding the implementation of data structures.dumplingshttps://software.codidact.com/users/547102021-12-07T18:09:56Z2022-06-26T16:39:47Z<p>I'm attempting a <a href="https://software.codidact.com/uploads/wxoGToBH6tVpJFBLwBv7P566">question</a> to do with data structures, file streams & linked lists. The code isn't complete yet as...https://software.codidact.com/posts/285298How can I modify the code above to accept string as user input and use strcmp to compare with the contents of the text file & then delete that line?dumplingshttps://software.codidact.com/users/547102021-12-18T13:57:43Z2022-06-26T16:36:27Z<p>I want to enter a string to compare with the text file, and if that word matches, then I want to delete that line containing that string.
How can I modify the code below, since the code below t...https://software.codidact.com/posts/285222Problems with data structures and filestreams.dumplingshttps://software.codidact.com/users/547102021-12-13T15:29:55Z2022-06-26T16:24:17Z<p>So I just started learning how to use file-streams in C & decided to attempt a question which is to do with library management in C, however I am currently encountering some problems and fee...https://software.codidact.com/posts/285135Why does fopen return NULL?dumplingshttps://software.codidact.com/users/547102021-12-07T10:56:47Z2022-06-26T14:20:36Z<pre><code class="language-c">#include<stdio.h>
#include<stdlib.h>
int main (void){
FILE *cfPtr;
char name[15];
int ID;
if ((cfPtr = fopen("draft.txt","w")) == NU...https://software.codidact.com/posts/286578array of arrays vs array of pointers to store array of string literalsalxhttps://software.codidact.com/users/539372022-06-11T09:26:11Z2022-06-11T22:26:33Z<p>Let's consider the following code:</p>
<pre><code class="language-c">const char a[][4] = {"aa", "aaa"};
const char *b[] = {"bb", "bbb"};
const char *const c[] = {"cc", "ccc"};
</code></pre>
...https://software.codidact.com/posts/286440Child process works only once after the parent's two calls to scanfLuca_Impellizzerihttps://software.codidact.com/users/566122022-05-13T13:38:45Z2022-05-24T06:52:35Z<p>This program creates a child process and shares two integers (<code>base</code> and <code>height</code>) through the shared memory.</p>
<p>The parent process asks four times to insert two integ...https://software.codidact.com/posts/286302Is `-isystem` a POSIX cc option?alxhttps://software.codidact.com/users/539372022-04-22T21:48:31Z2022-04-25T11:35:58Z<p>Is <code>-isystem/path/to/sys/includes</code> a standard compiler option, or is it a compiler extension implemented by gcc, clang, and maybe other compilers? Can I rely on its availability?</p>...https://software.codidact.com/posts/286190Should I check if pointer parameters are null pointers?Lundinhttps://software.codidact.com/users/81762022-04-05T09:11:08Z2022-04-06T09:56:12Z<p>When writing any form of custom function such as this:</p>
<pre><code>void func (int* a, int* b)
</code></pre>
<p>Should I add code to check if <code>a</code> and <code>b</code> are null poin...https://software.codidact.com/posts/286187Which functions in the C standard library must always be avoided?Lundinhttps://software.codidact.com/users/81762022-04-05T08:06:34Z2022-04-05T08:41:55Z<p>It would seem that the C standard library is a collection of diverse functions that pretty much just ended up in the standard by tradition or "accident", rather than through some careful plan or...https://software.codidact.com/posts/285104What does a variable followed by parentheses ("ptr()") mean?dumplingshttps://software.codidact.com/users/547102021-12-05T14:24:35Z2022-03-31T11:50:30Z<p>What does <code>ptr()</code> mean in this code?</p>
<pre><code class="language-c">#include<stdio.h>
#include<stdlib.h>
void PrintHello()
{
printf("Hello\n");
}
int Ad...https://software.codidact.com/posts/285972noreturn function with non-void return typealxhttps://software.codidact.com/users/539372022-02-17T12:39:11Z2022-02-17T21:12:03Z<p>Is it legal ISO C to declare a function as <code>noreturn</code> with a non-<code>void</code> return type (but of course not actually returning)?</p>
<p>As far as I can read from the standard, ...https://software.codidact.com/posts/285965Is it OK to use scanf with a void pointer?hkotsubohttps://software.codidact.com/users/530782022-02-16T14:51:07Z2022-02-17T07:18:31Z<p>I've created a function that calls <code>scanf</code> passing a void pointer as argument:</p>
<pre><code class="language-c">void read(const char *format, void *p) {
scanf(format, p);
}
<...https://software.codidact.com/posts/285956When does it not work to dereference the pointer for sizeof during malloc?klutthttps://software.codidact.com/users/81962022-02-14T13:41:00Z2022-02-16T15:48:05Z<h3>Background</h3>
<p>This is kind of a subquestion to <a href="https://software.codidact.com/posts/285898">How to properly use malloc?</a></p>
<p>When allocating, there are basically two common...https://software.codidact.com/posts/285896Are static pointers implicitly initialized to NULL?klutthttps://software.codidact.com/users/81962022-02-10T00:48:14Z2022-02-10T15:39:49Z<p>Consider this code:</p>
<pre><code>#include <stdio.h>
int main(void) {
static void *ptr;
if(ptr == NULL) puts("It's NULL!");
}
</code></pre>
<p>I wonder if this is guarant...https://software.codidact.com/posts/285579C Language Standard Linking SpecificationsJosh Hyatthttps://software.codidact.com/users/536292022-01-13T20:51:05Z2022-01-14T21:48:58Z<p>What (if anything) does the C standard have to say about linking objects? My guess is that, because C only defines language->behavior rules, it completely ignores any potential intermediate f...https://software.codidact.com/posts/285573Question regarding an error message in my compiler to do with my code on linked list.hamburgersarecoolhttps://software.codidact.com/users/547202022-01-13T14:36:29Z2022-01-14T04:41:40Z<p>Can anyone help me, I'm currently learning pointers, this is the code I wrote to try and insert a node at the beginning of the list. However at the part where I included the comment of "error at...https://software.codidact.com/posts/285350What is malloc's standard-defined behavior with respect to the amount of memory it allocates?ajvhttps://software.codidact.com/users/532422021-12-21T17:03:01Z2022-01-12T01:13:35Z<p>I recently told a friend that <code>malloc(n)</code> allocates and returns a pointer to a block of <em>at least N</em> bytes of memory, as opposed to <em>exactly N</em>; that it is allowed to al...https://software.codidact.com/posts/285225Behavior of Pointer Arithmetic on the StackJosh Hyatthttps://software.codidact.com/users/536292021-12-13T22:33:58Z2021-12-24T14:15:59Z<p>Consider the following code:</p>
<pre><code>#include <stdio.h>
int main() {
int a = 5;
int b;
++*(&b + 1);
printf("%d\n", a);
return 0;
}
</code></pre>
...https://software.codidact.com/posts/285177Regarding the heap sort algorithm.hamburgersarecoolhttps://software.codidact.com/users/547202021-12-09T09:43:42Z2021-12-21T18:30:57Z<p>I get the concept of the heap sort algorithm and its like first you have a heap(ordered binary tree) then we have the Max heap which has the highest element value in the array at the top of the ...https://software.codidact.com/posts/285204How to clear the contents of a file?dumplingshttps://software.codidact.com/users/547102021-12-11T03:57:53Z2021-12-11T10:46:53Z<p>How to clear the contents of a file using C?</p>
<!-- g: js, mdit -->