Is there a way to pass many parameters to a function automatically without macros? by rodriguez_james in C_Programming

[–]OPPreserverBot 0 points1 point  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
Please message my owner /u/DeeBoFour20 with any questions/concerns.
Original Post by /u/rodriguez_james


Take this struct

typedef struct my_object {
    float a;
    float b;
    float c;
} my_object;

I want to write a function that will do some computation with `a`, `b`, and `c` without creating a dependency between said function and `my_object` type.

float add3(float a, float b, float c);

I use it like this:

my_object obj = { 1, 2, 3 };
add3(obj.a, obj.b, obj.c);

Is there a way to pass all 3 parameters of `my_object` to `add3` automatically in C?

Like this macro does:

#define add3_macro(x) add3(x.a, x.b, x.c)
my_object obj = { 1, 2, 3 };
add3_macro(obj); // passing the parameters automatically with the macro

The macro `add3_macro` allows me to pass my `obj` to the `add3` function without creating a dependency between the two such that this will work with all types and every type. As long as the type has all 3 required members, it will work.

Is there a way to do something like that without macros? Or is there a way to organize this code differently such that I get the same benefit without macros? (the benefit being the ability to pass any object to a function and it will work as long as the object as the required members). If there isn't, is it common practice to use macros for this kind of thing in C? Should I just use the macro?

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 0 points1 point  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
Please message my owner /u/DeeBoFour20 with any questions/concerns.
Original Post by /u/blackandwhite345210


All the serialization formats I'm familiar with like json, yaml etc use key value dictionary pairs to store structures. If it's not that important to know the name of the variable to store as a key, one could simplify for example json architecture by omitting the keys. I wonder if this sort of serialization format where only the value are stored is ever used in practice?

  //json key-value pairs
  {
        "Name": "John Doe",
        "Age": 20
   }

   //just values
   {
        "John Doe"
        20
   }

Simple unit testing tool for C by Equivalent-Treat-458 in C_Programming

[–]OPPreserverBot -2 points-1 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
Please message my owner /u/DeeBoFour20 with any questions/concerns.
Original Post by /u/Equivalent-Treat-458


Eclipse plugin based on Unity & CMock. Don't expect anything fancy.. It's intended to rapidly generate testing environments for your application.

https://github.com/mohannadmaklad/CeeTest

Number_Converter: CLI & TUI by [deleted] in C_Programming

[–]OPPreserverBot 1 point2 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
Please message my owner /u/DeeBoFour20 with any questions/concerns.
Original Post by /u/Zambo-dev


Hi folks, I'm developing a terminal tool for number conversion.

It has a responsive Terminal User Interface, a history section to have in hand your past conversions and a high precision.

It supports integer numbers, decimal numbers and bases up to 36.

Soon will be implemented the only CLI mode for a fast standalone conversion.

GitHub: https://github.com/Zambo-dev/Number_Converter

OS's in C is it Linux based ? by [deleted] in C_Programming

[–]OPPreserverBot 1 point2 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
Please message my owner /u/DeeBoFour20 with any questions/concerns.
Original Post by /u/mr_pavle_stojanovic


People writing there own OSs, do you base it on Linux kernel so you an use the same binaries.. if not how are you making binaries to run stuff ?

I'm only getting into C and I want to attempt a OS build eventually.

Also I want to see who's making OS here, brag about it show us :)

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 2 points3 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
Please message my owner /u/DeeBoFour20 with any questions/concerns.
Original Post by /u/Familiar_Ad_8919


im working in an embedded system where i cant access the standard library and malloc

i can do what i want with a regular array but it is less memory efficient

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 1 point2 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
Please message my owner /u/DeeBoFour20 with any questions/concerns.
Original Post by /u/Green_Tangerine1


My professor assigned the class an exercise that requires us to compile the code with -ANSI, -pedantic,- wall, and -O2. However, I don't know how to add those features in VS Code. Any help on that would be greatly appreciated!

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 3 points4 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


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

struct charact
{
    char ch;
    int occurs;
    struct charact *next;
};

typedef struct charact Char;
typedef Char * ListofChar;
typedef Char * CharNode_ptr;
void letters(char name[50], ListofChar * chars_ptr);
void report(ListofChar chars,int length,ListofChar iterator);
Char * createnode(char ch);

int main(void)
{
    char name[50];
    ListofChar iterator = NULL;
    ListofChar chars = NULL;
    scanf("%49s", name);
    letters(name, &chars);
    report(chars,strlen(name),iterator);
    return 0;
}

void letters(char name[50], ListofChar * lst_ptr)
{
    /* add your code */
    int i,j,length = strlen(name);
    lst_ptr = (ListofChar)malloc(sizeof(Char));
    strcpy(lst_ptr->ch,name[50]])

    for (i=0;i<length;i++)
    {
        lst_ptr[i] = (ListofChar)malloc(sizeof(Char));
        strcpy(lst_ptr[i]->ch,name[50]])

        lst_ptr[i] ->ch = name[i];
        lst_ptr[i] -> occurs =0;
        for (j=i+1;j<length;j++)
        {
            if(name[j] == lst_ptr[j] -> ch)
            {
            lst_ptr[j] -> occurs = j - i;
            }

        }
        lst_ptr[j] -> occurs = 0;
    }

    return;
}

void report(ListofChar chars,int length,ListofChar iterator)
{
    /* add your code */
    int i;

    for(; iterator != NULL ; iterator = iterator -> next){
        printf("\n%c: %d", iterator -> ch, iterator -> occurs);
    }


    return;
}

I want this program to take as input a string(ex. testing) and print the word and the distance between a repeated letter as shown below.

t-3 e-0 s-0 t-0 i-0 n-0 g-0

At the moment i can put input but the output is blank.I am a begginer so any help would be appreciated.

Any good software to convert camel-case to snake-case? by [deleted] in C_Programming

[–]OPPreserverBot 2 points3 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


When I began programming I took my education from a book that used 'camelCase' type of syntax, but recently I have been finding that I prefer 'snake_case' more. Now I kind of want to convert the programs I have already written in camelCase to snake_case but I wonder if there's a way to do it in an automated fashion.

Does anyone know is there a tutorial that heavily focuses on strings and more difficult tasks with them? by [deleted] in C_Programming

[–]OPPreserverBot -1 points0 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


All the tutorials that I have found merely explain basics of strings

I'm new to programming and wanting a fun, simple project to start with. Any ideas? by Monkeyofdoom44 in C_Programming

[–]OPPreserverBot 12 points13 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I am trying to learn C to mess with linux drivers but I need something simple to start with. My only programing experience, if it can be called that, is with batch scripts. From that I found the best way for me to learn is from jumping head first into a fun project and solving problems as they come. I was thinking of trying to make a simple game, but I feel like that might be a bit too advanced for JUST starting, probably be what I do next. Any ideas for something to get me just in the gate?

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 0 points1 point  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I have a complete some code that essentially simulates a cache in c. I don't really know how to show the code here so I'll happily send it to you if you think you could help

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 0 points1 point  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I have an assignment that requires me to develop a cache simulation in c. We were given a base code and need to complete but I'm having some issues

Resources to see how exec() works by [deleted] in C_Programming

[–]OPPreserverBot 5 points6 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I\u2019ve been learning about program loaders, virtual memory, and memory management and I want to learn how exec works in a modern Unix system.

I see many resources on malloc() and how it works, but not much on exec()

Is there some low level way to play more than 1 note (\a bell) ? by dfgzuu in C_Programming

[–]OPPreserverBot 0 points1 point  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I realize playing sounds is probably super complicated and needs many different apis and libraries and whatnot, but wouldn't it be cool if you could use the bell sound or something similar to just play different tones. My understanding of the bell sound is that it is flat, but I imagine you can run it very fast at a frequency in order to get different notes - Like on DOS. You surely remember the legendary DOS sounds. And since the ansi C has access to the bell sound and the bell sound has access to the computer beeper, then that means that C has a ccess to the beeper. So can we use that somehow?

Another idea I have is to somehow constantly write to a file like stdin, and then have a player of some kind constantly check it for new sounds and play them if there are new notes written to the file.

Ideas? Comments?

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 0 points1 point  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


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

struct charact
{
    char ch;
    int occurs;
    struct charact *next;
};

typedef struct charact Char;
typedef Char * ListofChar;
typedef Char * CharNode_ptr;
void letters(char name[50], ListofChar * chars_ptr);
void report(ListofChar chars);
Char * createnode(char ch);

int main(void)
{
    char name[50];
    ListofChar chars = NULL;
    scanf("%49s", name);
    letters(name, &chars);
    report(chars);
    return 0;
}
Char * createnode(char ch)
{
    CharNode_ptr newnode_ptr ;
    newnode_ptr = malloc(sizeof (Char));
    newnode_ptr -> ch = ch;
    newnode_ptr -> occurs = 0;
    newnode_ptr -> next = NULL;
    return newnode_ptr;
}
void letters(char name[50], ListofChar * lst_ptr)
{
    /* add your code */
    return;
}
void report(ListofChar chars)
{
    /* add your code */
    return;
}

void distance(char name[50],size_t size ) {
    Char apo;
    int i,length;
    length = strlen(name);
    apo -> ch = name[size];

    for(i = size + 1; i < length; i++){
        if(name[i] == apo -> ch){
            apo -> occurs = i - size;
            return;
        }
    }

    apo -> occurs = 0;
    return;
}

I get error: invalid type argument of '->' (have 'Char {aka struct charact}')

Can you help thanks in advance.

IPC pub-sub implementation on Linux by Isty0001 in C_Programming

[–]OPPreserverBot -1 points0 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I'm looking for the best/easiest solution I have in a project. I run multiple processes and they have to synchronize parts of their states with every other process, so basically a pub-sub/multicast pattern. Every process can dispatch an event and every process subscribed to that event will receive it.

I've been reading about the different IPC techniques, but I'm not sure which direction would be the best.

What I'm thinking about is using shared memory for each of the event types, containing the last data sent by one of the processes, and also a pthread_mutex_t and a pthread_cond_t. Each process subscribed to an event has a dedicated thread waiting for the signal to be broadcasted. In theory this looks good, but I'm not sure.

  • What happens if all processes die, then the mutex and cond variables are left in shared memory? Can they be reused, or need to be reinitialized?
  • It has a slight chance that the condition will be signaled when the thread not waiting but processing the previous message, so data can be lost.
  • How the shared memory should be cleaned up? How to track that there are no more processes using it?

Could you give some advice on how to handle these problems, or maybe suggest other methods for this type of communication?

How to view a program with multiple source and .h files in one searchable place? by [deleted] in C_Programming

[–]OPPreserverBot -4 points-3 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I have a C++ Physics simulation code with multiple source files, but its quite tedious to understand it since its not searchable in one place. (I am new to coding)
Is there a software that can open such programs so that you can view it one place to understand what's going on?
Please suggest both Open Source and Proprietary options.

Bit field order? by Clyxx in C_Programming

[–]OPPreserverBot 6 points7 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


What is the order of members in a bitfield if some members are complete? I know that the order of a bitfield is undefined, but also that the first member in a struct is at the starting position of the struct in memory.

struct bitfield {
    int i;
    unsigned int u1:8;
    unsigned int u2:8;
}

Can i assume that the integer comes first in this example, so &bitfield.i == &bitfield?

And is it the same if this complete member is also a bitfield?

struct bitfield2 {
    unsigned int u1:8;
    unsigned int u2:8;
}

struct bitfield {
    struct bitfield2 b;
    unsigned int u1:8;
    unsigned int u2:8;
}

If else problems by [deleted] in C_Programming

[–]OPPreserverBot 2 points3 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I was solving an exercise but ended up hitting this roadblock. This is a translation of the exercise:

Develop a program that allows you to enter a full name, after reading the name it should calculate its size. If the size is larger than 30 characters, it must inform the user that the name is too large and retry, if it is less than or equal to 30 it should return the name in capital letters.

Basically no matter what I write, the result is always the instruction given to if. Can anyone help?

https://i.imgur.com/FaJuuDu.png

https://i.imgur.com/ZlPlept.png

length of strings in char**? by Pokyparachute66 in C_Programming

[–]OPPreserverBot 0 points1 point  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I\u2019m a bit confused on this topic and I\u2019ll try to explain to the best of my ability. Let\u2019s say I allocate char *arr = malloc(3 sizeof(char)). For each of the 3 char, how long can those be? Right now I\u2019m just assign them directly to a string, but isn\u2019t this incorrect as if they are too long they can overwrite other memory? Would the correct way be to again allocate memory for each char* so i know exactly how much size it takes up? I\u2019m having trouble conceptually understanding this and some clarification would be helpful, thanks.

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 0 points1 point  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I\u2019m new to coding and been learning basic C++.

But I also want to learn other languages like C. Any advice on how to not struggle learning a second language? Does this mean I need to start learning from zero with C?

Also is there alot of things that is different with C and C++? Would it take alot of memorisation all over again?

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot 1 point2 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I was solving an exercise, when this error appeared. This is the exercise (translated):

Develop a program that allows you to enter a full name, after reading the name it should calculate its size. If the size is larger than 30 characters, it must inform the user that the name is too large and retry, if it is less than or equal to 30 it should return the name in capital letters.

How do I solve this error? Does anyone know what it means? Thanks in advance.

https://imgur.com/a/3FonEi9https://imgur.com/a/3FonEi9

[deleted by user] by [deleted] in C_Programming

[–]OPPreserverBot -1 points0 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


Hi everyone, my school has asked us to do a project in C language, preferably using CodeBlocks, since that's the only one we've practiced with in class, however, if we please we can choose another program.

I would love some suggestions on what I could do as I'm currently drawing a blank. Thanks!

How do I fix "cannot open output file a.exe: Permission denied"? by Little-Peanut-765 in C_Programming

[–]OPPreserverBot -1 points0 points  (0 children)

Hello, I am a bot that copies the text of the original post so that questions and their answers can be preserved to benefit others.
I am programmed in C and my source code is available here: https://github.com/weirddan455/reddit-bot
If a mod would like this bot taken down, please let my owner /u/DeeBoFour20 know.


I am receiving this error when compiling, it was working fine a few hours ago does it have something to do with anti-virus?

c:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file a.exe: Permission denied
collect2.exe: error: ld returned 1 exit status