Get PID of the signal sender in C [on hold]

I’ve been searching for a way to return the pid of the signal sender’s process. Here’s my code

void get_pid(int sig, siginfo_t *info, void *context)
        printf("PID of signal sender = %d\n", info->si_pid);

int main(int argc, char **argv)
        struct sigaction        sa;

        printf("PID: %d\n", getpid());       //display PID for kill()
        sa.sa_flags = SA_SIGINFO;
        sa.sa_sigaction = get_pid;
        sigaction(SIGUSR1, &sa, NULL);
        pause();                            //wait for a signal

This program first displays its PID so another program can use kill() to send it a signal. Then it calls pause() so the program doesn’t end before it gets a signal.

How can I access the sender’s PID from the main function.

Configure automake to target assembly

Is it possible to configure automake to generate a Makefile which, in addition to building the .o files and linked binary, also has targets for %.s? I want to be able to review the compiler output in a text format without having to invoke binutils on the .o files.

Specifically, if I have main.c as a source file, I want to be able to run make main.s. The desired recipe would be the same as that for main.o, but using CC1 := $(CC) -S.

C const pointer to const struct array as function argument

How can i make Alt.1 to work as intended by passing a pointer of an array and get the requested reference of an array in Alt.1 ?

struct mystruct
    int id1;
    int id2;

const struct mystruct local_struct[] = {
    {0, 55},
    {1, 66},

// Alt.1 i like to make this work (not working)
int get_reference_1(const struct mystruct *s){

   s = local_struct;
   return 0;

// Alt.2 works perfect but i like to use the return as status as in Alt.1.
const struct mystruct *get_reference_2(){
   return local_struct;

int main()
  struct mystruct *s = NULL;

  // Alt.1
     /* Expected Fail*/
     /* Expected Success*/

  // Alt.2
  s = get_reference_2()
     /* Expected Fail*/
     /* Expected Success*/

  return 0;

Maybe i’m thinking wrong and i need to pass a double pointer?

Edit: Corrected with ‘const’.
Edit2: Updated header.

Caesar Cipher Program to Encrypt/Decrypt a paragraph

First off, I am a newcomer to programming, so be gentle. Also I have been working on this assignment to no avail. The assignment is to create a Caesar Cipher program which encrypts or decrypts a paragraph up to 100 characters. It’s actually two separate labs. The first lab is to encrypt, then the second lab is to decrypt. Once I figure out how to create the encryption program, the decryption program should be simple as I can just make semantic changes to decrypt instead of encrypt. Anyway here is the code I have so far. It passed 4 of the 5 tests they give us, but for some reason there is one test where the final character is an ‘@’ symbol. This makes no sense to me as it does not happen in any other tests, and I believe that my code places a ‘\0’ symbol at the of the string, so ‘@’ should not be showing up in this one test.

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>

// ---------------------- DO NOT MODIFY THIS SECTION -----------------------    ---------
#define MAX_WORD_LENGHT 20

int main(void) {   
// definitions
char plaintext[MAX_PGRAPH_LENGTH] = "";
char ciphertext[MAX_PGRAPH_LENGTH];
char input[MAX_WORD_LENGHT];    

// read the key
int key;
scanf("Key: %d, ", &key);

// read text
scanf("Input: ");
while (true)
    scanf("%s", input);
    if (strlen(plaintext) + strlen(input) + 1 > MAX_PGRAPH_LENGTH)

    strcat(plaintext, input);
    strcat(plaintext, " ");
plaintext[strlen(plaintext) - 1] = '\0';
// ---------------------- --------------------------------------------------    ---------

int i;

for(i = 0; i < strlen(plaintext); ++i) 

  if(plaintext[i] >= 'a' && plaintext[i] <= 'z') {
     ciphertext[i] = ((plaintext[i] + (key % 26) - 97) % 26) + 97; 

     if(ciphertext[i] > 'z') {
        ciphertext[i] = ((plaintext[i] + (key % 26) - 97) % 26) + 97 - 26; }

  else if(plaintext[i] >= 'A' && plaintext[i] <= 'Z') {
     ciphertext[i] = ((plaintext[i] + (key % 26) - 'A') % 26) + 'A';

     if(ciphertext[i] > 'Z') {
        ciphertext[i] = ((plaintext[i] + (key % 26) - 'A') % 26) + 'A' - 26; }

  else {
     ciphertext[i] = plaintext[i]; }

 for(i = 0; i < strlen(plaintext) && plaintext[i] == '\0'; ++i) {
  ciphertext[i] = '\0'; }

// ---------------------- DO NOT MODIFY THIS SECTION -----------------------    ---------
printf("   Key: %d\n", key);
printf(" Input: %s\n", plaintext); 
printf("Output: %s\n", ciphertext);
// ---------------------- --------------------------------------------------    ---------

As you can see, the lab gave us the setup and multiple blocks of code, and I am responsible for the actual Caesar cipher portion of the coding. My question is, am I not properly setting the ‘\0’ at the last position in C? One thing I noticed as well is that if I type an input such as “The dog ran”, when input is printed to the screen, it will print “The dog ran ran ran ran ran..” until it hits 100 characters. Luckily, all of the tests they give us to run are paragraphs which exceed 100 characters, so I actually don’t have to worry about passing a test in which I’m encrypting less than 100 characters. But I would still like to know why my plaintext string is repeating the last input word over and over. Sorry for the ridiculously long post, I have tried everything though and do not know where I am going wrong.

Pointer to function with some, but not all arguments fixed

I want to use Brents method as present in the Numerical Recepies Book to minimize a function. The signature of the minimzation routine essentially looks like this:

float brent(float (*f)(float), float *xmin, "other parameters not relevant to question")

As you can guess brent returns the minimum value of f and stores its argument in xmin.
However, the exact form of the function I want to minimize depends on additional parameters. Say

float minimize_me(float x, float a, float b)

Once I decided on the values of a and b I want to minimize it with respect to x.

I could simply add additional arguments to all functions called, all the way down to brent, thus changing its signature to

float brent(float (*f)(float,float,float),float a ,float b , float *xmin, ...)

and consequently call (*f)(x,a,b) inside brent every time. This, however, seems not really elegant to me since I would now have to pass not only the pointer to minimize_me, but also two additional parameters down a whole chain of functions.

I suspect there could be a more elegant solution, like creating a pointer to a version of the function with a and b as fixed values.

Even if it is a really obscure solution, please don’t keep it from me, since I feel it could improve my overall understanding of the language.

ffmpeg libav swap output file [on hold]

I writing streaming video into disk, and I want to swap output file

while(!stop) {
    int error = av_read_frame(formatCtxPtr_.get(), packetPtr.get());
    if (error<0)
    if (packetPtr->stream_index == videoStreamIndex_) {
        AVPacket *ppkt = av_packet_clone(packetPtr.get());
        AVPacket& pkt = *ppkt;
        auto in_stream = formatCtxPtr_->streams[pkt.stream_index];
        pkt.stream_index = 0 ; //Only on output stream video index = 0
        auto out_stream = fmtCtxOutputPtr_->streams[pkt.stream_index];
        pkt.pos = -1;
        int ret = av_interleaved_write_frame( fmtCtxOutputPtr_.get(), &pkt );
        if ( swap_ /*&& (AV_PKT_FLAG_KEY & pkt.flags)*/ ) { //Never have keyframe
            swap_ = false ;
            //Reset stream time
            out_stream->start_time = out_stream->duration = out_stream->first_dts = out_stream->cur_dts = out_stream->info->last_dts = AV_NOPTS_VALUE ;
            int error = avio_open(&fmtCtxOutputPtr_->pb, "stream2.mp4", AVIO_FLAG_WRITE);
            error = avformat_write_header( fmtCtxOutputPtr_.get(), NULL );              

I have not error, write_frame return 0, “stream2.mp4” is create but still at 0 byte

In a C Linked List why are the nodes also pointers? [duplicate]

This question already has an answer here:

I could not grasp the reason we create pointers of nodes instead of node structures when we try to implement linked lists as here:

typedef struct node {
    int val;
    struct node * next;
} node_t;


node_t * head = NULL;
head = malloc(sizeof(node_t));
if (head == NULL) {
    return 1;

head->val = 1;
head->next = NULL;

here, why do we declare nodes such as head as pointers of structures instead of direct structures>

Valgrind error in C code with icd pointers

I inherited a C code.. and one of the lines is below. What is meant by icd here. Is it a keyword or just a name. I could not find it on google anywhere.

icd **d1cols = (icd**)malloc(measquant*sizeof(icd *));

There after I have:

for(i=0; i<measquant; i++){
    *(d1cols + i) = getwtOnAFileColReturnDouble1(fp1, fp2, xx, yy);

If I print the below line

printf("Test 0,0 weight=%.3le\n",*((*(d1cols + 0 ))->wt+0));

I am getting below errors in valgrind among other errors:

Invalid read of size 8

==45918== at 0x401945: main (getwtmeasAll_1_main.c:143)
==45918== Address 0x7e83850 is 0 bytes inside a block of size 896 free’d

==45918== at 0x4C27430: free (vg_replace_malloc.c:446)

What is wrong here.

C program crashes after pointer arithmetic (only in some computers)

I have the following code (I didn’t write it and it is simplified to only show the problematic part):

#include <stdlib.h>

typedef struct test_struct {
    unsigned int foo;
    char *dummy;
} test_struct;

int main()
    test_struct *s = (test_struct *) malloc(10 * sizeof(test_struct));
    s = (test_struct *)((unsigned long)s + 16);
    s->foo = 1; // crash!

The program allocates memory for 10 structs (10*24 bytes in my platform). Then, the pointer gets an addition of 16 bytes, and it tries to write a number in that position.

I have tested this snippet in 4 computers. Two of them are running on Windows 7 x64, and it works well. Another one running on lubuntu x64, and works as expected, too. The other one is a Windows 10 x64, and it crashes.

Could you help me to understand what is wrong in those lines? I’m using a third party library that does this and I don’t know what is really happening.

Use of void*, void ** and variadic function

I want to write a function that frees as many pointers as wanted.
So I have this one :

void    myfree(size_t n, ...)
    void    **del;
    va_list ap;

    va_start(ap, n);
    while (n > 0)
        del = va_arg(ap, void **);
        *del = NULL;

I call it like that :

char *one = malloc(x);
sometype_t *two = malloc(x);
myfree(2, &one, &two);

I assume that one and two are now pointing to NULL.
It seems to be working, but I’m still a bit worried. I did some researches about void** but I’m not sure if my function is really valid (UB are you there ?)

During my researches and tests, I tried some things I do not really understand.

Let’s say we have this function

void    f(void **ptr)

If I call it like this

int *intptr = NULL;

I get a compiler warning : warning: incompatible pointer types passing 'int **' to parameter of type 'void **'

So I tried this

int *intptr = NULL;
f( & ((void *)intptr) );

Compiler error : error: cannot take the address of an rvalue of type 'void *'

But if I do

void *voidptr = NULL;
f(&voidptr); // I can take the address of a void pointer right ?

Actually void*, void** and conversions/casts with them are still unclear to me. That is why I’m worried about myfree() I posted above.
If any of you have some explanations I would be grateful 🙂