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 8237299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:25:42+00:00 2026-06-07T19:25:42+00:00

I want to transfer few variables using structure. Following is the sample program code.

  • 0

I want to transfer few variables using structure. Following is the sample program code. When I run this program, I get segmentation fault. I use gcc compiler.

Can anyone help me with this?

struct data{
    const char * ip;
    const char * address;
    const char * name;
};

int fun(struct data *arg) {
    //struct data *all;
    const char *name = arg->name;
    printf("\n My name is:%s",name);
    return 1; 
}


int main(int argc, char * const argv[]) {
    struct data * all;   
    int k=0;
    //data.name = argv[1];

    all->name=argv[1];
    k = fun(all);

    printf("\n k is:%d ",k);

    return 0;
}
  • 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-06-07T19:25:45+00:00Added an answer on June 7, 2026 at 7:25 pm

    The problem is here:

    struct data * all;
    all->name=argv[1];
    

    You have not allocated memory for all. When you have an uninitialized pointer, it is pointing to random locations in the memory, which you probably won’t have access to. You have two options:

    1. Allocate on the stack:

      struct data all;
      all.name=argv[1];
      k = fun(&all);
      
    2. Allocate on the heap:

      struct data *all = malloc(sizeof(*all));
      if (all != NULL)
      {
          all->name=argv[1];
          k = fun(all);
      }
      free(all);
      

    The first case is good when you know that all will be needed only in the current function (and those you call). Therefore, allocating it on the stack is sufficient.

    The second case is good for when you need all outside the function creating it, for example when you are returning it. Imagine a function initializing all and return it for others to use. In such a case, you can’t allocate it on the stack, since it will get destroyed after the function returns.

    You can read more about it in this question.

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

Sidebar

Related Questions

I want to transfer an ROI instead of the full Image using Camera. This
I want to transfer files across the network using C or C++. What topics
I want to transfer data(21M rows) from mysql database to DynamoDB. I am using
I am writing a message transfer program between multiple clients and server. I want
I have this. I want to click and transfer the clicked data to another
I want to transfer the following statement to SubSonic 2.2 SELECT b.* FROM tableA
I want transfer data from MongoDB to SQL Sever using the C# driver. The
I want to transfer data from a vertical db layout like this: --------------------- |
I want to transfer files from one directory to another using streamreader and writer
i want transfer files using p2p connection. There will be a server or any

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.