r/C_Programming 6h ago

Question Meaning of [restrict .n] in manpages?

12 Upvotes

Hello,

I'm looking at man-pages 6.7 installed in Ubuntu 26.04 and I notice the following in the memcpy page.

I guess that the restrict refers to the keyword restrict being integrated into LIBC, but what is this .n in dest and src? Does it mean that the dest and src pointers do not overlap on the first n bytes? Where is this syntax defined and what other interesting cases can be out there?

Thanks

SYNOPSIS

#include <string.h>

void *memcpy(void dest[restrict .n], const void src[restrict .n],

size_t n);


r/C_Programming 21h ago

Converting fractions to integers

6 Upvotes

I have a double* which has the following entries:

0.3333333333333334
0.6666666666666668
0.1249999999999999
1

Here, the last entry, 1, can be considered the right hand side of an inequality:

0.3333333333333334 x + 0.6666666666666668 y + 0.1249999999999999 z >= 1

These numbers come from a numerical linear algebra library over which I don't have any control. What is the easiest way to "convert" this to the following equivalent inequality (subject to a user provided tolerance of what counts as an epsilon so that epsilon within an integer is to be counted as an integer)?

8 x + 16 y + 3 z >= 24

Is there a package that does such conversion, if reasonably possible? I consider it unreasonably possible by multiplying everything in the original equation by a large enough power of 10. But I do not want that.


r/C_Programming 11h ago

Question Why isn't my code working?

5 Upvotes

I just started learning C(2 days ago) and as a first project I decided to make some data structures, starting with dynamic arrays. I made a struct called List and some functions for. The function setList() sets the value of an index of the array, if the index is larger that the current size of the array, it resizes it. However, when i tried to use in a for loop, it didn't work despite it working elsewhere.

#include <stdio.h>
#include <stdlib.h>


#define itirate(index, limit) for(int index = 0; index < limit; index++)


typedef struct 
{
    size_t size;
    int* arr;
} List;


List* newList (size_t size) 
{
    List *newone = malloc(sizeof(List));
    newone->arr = calloc(size, sizeof(int));
    newone->size = size;
    return newone;
}


void setList(List* list, int index, int value) 
{
    if (index >= list->size)
    {
        list->arr = realloc(list->arr, index + 1 * sizeof(int));
        list->size = index + 1;
    }


    list->arr[index] = value;
}


int main() 
{
    
    List *mok = newList(5);

    itirate(i, 5) setList(mok, i, i);
    itirate(i, 5) printf("%d\n", mok->arr[i]);

    //setList(mok, 13, 9); this works
    //printf("%d\n", mok->arr[13]);

    for(int i = 5; i < 10; i++) setList(mok, i, i); // this does not somehow
    for(int i = 5; i < 10; i++) printf("%d\n", mok->arr[i]);
    
    return 0;
}

r/C_Programming 13h ago

Question How process ids are generated

5 Upvotes

i know fork creates processes but how process ids are generated


r/C_Programming 6h ago

Question Working with arrays in functions

3 Upvotes

Hey everybody. I’m a beginner to C and I was writing some functions today to get used to doing things. I tried to write binary search and bubble sort. I tried to pass in an array as an argument to the functions, but the compiler gave me a bunch of warnings. I looked it up and I saw that passing in an array is the same as passing in its pointer. I haven’t touched pointers yet, but I have two questions:
1. If I dereferenced the pointer to an array, wouldn’t that return the same as indexing the first value?
2. If I wanted to pass in the entire array, could I do that by passing in the pointers of both the first and last elements and using pointer arithmetic to access the other elements? What’s the idiomatic way of doing this?


r/C_Programming 19h ago

Review Code Review Request: Windows Sudoku Game

3 Upvotes

Background
Hi, I am a 15-year-old teen (just so what you'd know what to expect) who left school because our school system taught us to be parrots. I want to be a god-level developer, not a code monkey. I wrote a Sudoku GUI using win32api in C Language.
It is currently working and does what it is supposed to do, but because I am still learning, I know it is likely inefficient and could be written much better. 

Code : https://github.com/reewdgh/sudoku_gui.

Concerns:
Please guide me on:

Bugs or potential issues
Efficiency
How can I move to Tier 1 to Tier 2
Naming anything I could simplify or improve.


r/C_Programming 19h ago

Question Wait... How does the stack work again?

0 Upvotes

I've been working in C and assembly for 3 years now (consistently) and I've noticed a trend. The more I work on C or do C adjacent activities like decompiling assembly, the quicker I forget how the stack and heap works.

And the level of forgetting is always proportional to how complicated the project I'm working on is. A basic project? Probably won't forget. An intermediary project? I'll need to Google "Stack vs Heap" at least once. Advanced project? I'll need to start from scratch and watch a YouTube video a couple of times to remember.

Does anyone else have this amnesia or is it just me?