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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:37:02+00:00 2026-06-06T06:37:02+00:00

There is struct like this. struct Address { int id; int set; char name[MAX_DATA];

  • 0

There is struct like this.

struct Address {
    int id;
    int set;
    char name[MAX_DATA];
    char email[MAX_DATA];
};

And function which set’s address.

void Database_set(struct Connection *conn, int id, const char *name, const char *email) {
    struct Address *addr = &conn->db->rows[id];
    if(addr->set) die("Address already set");

    addr->set = 1;
    char *res = strncpy(addr->name, name, MAX_DATA);
    if(!res) die("Name copy failed");

    *res = strncpy(addr->email, email, MAX_DATA);
    if(!res) die("Email copy failed");
}

But first character of addr->name gets corrupted after this line.

*res = strncpy(addr->email, email, MAX_DATA);

Any ideas?

  • 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-06T06:37:03+00:00Added an answer on June 6, 2026 at 6:37 am

    strncpy returns its first argument so after

    char *res = strncpy(addr->name, name, MAX_DATA);
    

    the variable res holds addr->name (equivalently, &(addr->name[0])) so when

    *res = strncpy(addr->email, email, MAX_DATA);
    

    is run it is the equivalent of

    addr->name[0] = strncpy(addr->email, email, MAX_DATA);
    

    This assignment is what corrupts the first char of addr->name. As Greg Hewgill says, you don’t need to check or even save the return value of strncpy.

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

Sidebar

Related Questions

Let's suppose I have a struct like this: struct my_struct { int a; int
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
(I've edited this question to avoid distractions. There is one core question which would
I am creating an application which uses a vCard struct . Currently, this struct
In particular, I'm thinking of a scenario like this: unsafe struct Foo { public
struct Item { int i; char * x; }; Item all[] = { {0,
I have a class like this: struct WorkItemResultData; template <typename ExecutionPolicy> class Engine {
I have read that if you declare two structs like this: struct Node {
Is a C# struct thread-safe? For example if there is a: struct Data {
is there a difference between a struct in c++ and a struct in c#?

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.