Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6829751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:32:12+00:00 2026-05-26T22:32:12+00:00

#include<stdio.h> #include<string.h> #define USER_MEM (10*1024) typedef struct { unsigned short int vol_level; int mute_stat;

  • 0
#include<stdio.h>
#include<string.h>

#define USER_MEM (10*1024)

typedef struct {
    unsigned short int vol_level;
    int mute_stat;
}audio_state;

static audio_state aud_stat;

static unsigned char user_mem[USER_MEM];

void aud_read(unsigned char * data)
{
    unsigned short pos =0;
    memcpy(data,&user_mem[pos],sizeof(data));
    printf("The Read data is:%c",*data);
}

void aud_write(unsigned char * data)
{
    unsigned short pos =0;
    memcpy(&user_mem[pos],data,sizeof(user_mem[pos]));
    printf("The written data is:%s",*data);
}

int main()
{  
    aud_stat.vol_level=10;
    aud_stat.mute_stat=20;

    aud_write((unsigned char*)&aud_stat);
    aud_read((unsigned char*)&aud_stat);
}

This program is throwing a segmentation fault. I wanted to read some bytes of data as well as to write some bytes of data. I have written the above code but it’s throwing an error as seg fault. Please help me to resolve this issue.

EDITED

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

#define USER_MEM (10*1024)

typedef struct {
    unsigned short int vol_level;
    int mute_stat;
}audio_state;

static audio_state aud_stat;

static unsigned char user_mem[USER_MEM];

void read(unsigned char * data,unsigned short num)
{
    printf("Into Read!\n");
    unsigned short pos =0;
    memcpy(data,&user_mem[pos],num);
    printf("The Read data is:%c",*data);
}

void write(unsigned char * data,unsigned short num)
{
    printf("Into Write!\n");
    unsigned short pos =0;
    memcpy(&user_mem[pos],data,num);
    printf("The written data is:%c",*data);
}

int main()
{
    aud_stat.vol_level=10;
    aud_stat.mute_stat=20;
    write((unsigned char*)&aud_stat,sizeof(audio_state));
    read((unsigned char*)&aud_stat,sizeof(audio_state));
}

output:

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:32:13+00:00Added an answer on May 26, 2026 at 10:32 pm

    First, your use of read() and write() shadows the system-supplied read(2) and write(2) routines. This is a giant mistake. (You can replace the system-supplied system call wrappers but you had better make sure you do as good a job programming them as the system C library authors did in the first place. Yours aren’t even close to what the system-supplied read(2) and write(2) functions do.) Your printf(3) call will try to use write(2) internally to write your output and will find your implementation instead. Because yours handles its parameters very differently than the write(2) implementation, it’ll probably die on that memcpy() call — you’ve dereferenced the first argument to write() as if it were a pointer, but printf(3) will call it with an integer like 1. Dereferencing 1 is a sure-fire way to segfault.

    Second, you cannot use sizeof on an array passed into a function as a parameter. Arrays passed as parameters decay to pointers — your function cannot determine if it was called with an array or a character pointer, and sizeof is going to calculate (at compile time!) the size of a pointer. Huge difference. Either pass array sizes in the parameters or use compile-time #defines to make them the same across the whole project.

    Third:

    void write(unsigned char * data)
    /* .... */
    printf("The written data is:%s",*data);
    

    This has the effect of passing a single character to printf(3) but your format string suggested you were going to pass a “string”. C strings are NUL-terminated char arrays — who knows when the next '\0' byte in the input you’ve given it is going to come.

    Fourth:

    void write(unsigned char * data)
    /* ... */
    aud_stat.mute_stat=20;
    write((unsigned char*)&aud_stat);
    

    You’re making dangerous (and needless) casts away from your structure type to a completely unrelated type. Your new write() replacement should look something more like void write_aud(audio_state *a), so you can work with your objects directly.

    I strongly recommend reading The C Programming Language by Kernighan and Ritchie before spending much more time on this program — trying to debug this one into existence is going be a painfully slow way to learn C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include<stdio.h> #include<zlib.h> #include<unistd.h> #include<string.h> int main(int argc, char *argv[]) { char *path=NULL; size_t size;
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
The following code outputs Illegal seek: #include <stdio.h> #include <errno.h> #include <string.h> int main()
I write a program with libcurl. #include <stdio.h> #include <string.h> #include <curl/curl.h> #define URL_MAX
#include <stdio.h> #include<stdlib.h> #define LIST.H onus; int main () { char *p,*s; printf( LIST.H
#include<stdio.h> #include<stdlib.h> #define n ((sizeof(char)) * 100 ) int stringlength(char * str) { int
#include<stdio.h> #include<ctype.h> #include<string.h> /* this is a lexer which recognizes constants , variables ,symbols,
So I've got some C code: #include <stdio.h> #include <string.h> /* putting one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.