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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:42:07+00:00 2026-05-28T07:42:07+00:00

i’m dealing with memcpy to copy one array into another: here is the code,

  • 0

i’m dealing with memcpy to copy one array into another: here is the code, (i gave also the declarations)

.... /* s is a struct with int *t_con_cust;  */
/* bad to use an equivalent name i guess ..*/
s.t_con_cust = malloc(nb_cust*sizeof(*s.t_con_cust));
int *t_con_cust = malloc(nb_cust*sizeof(*t_con_cust));
...    
t_con_cust[0] = 1;
memcpy(s.t_con_cust, t_con_cust, nb_cust*sizeof(int));

fprintf(stdout, "s.t_con_cust[0] -> %d \n", s.t_con_cust[0]);
t_con_cust[0] = 0;
fprintf(stdout, "s.t_con_cust[0] -> %d \n", s.t_con_cust[0]);

The execution gave me :

s.t_con_cust[0] -> 1

s.t_con_cust[0] -> 0

which is beyond me, because when I tried with "sample code" like that

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

typedef struct { 
    int *dest;
} lambda_struct;


int main(void) { 
    lambda_struct s;
    s.dest = malloc(10*sizeof(*s.dest)); 
    int *src  = malloc(10*sizeof(*src));
    int i;

    for(i = 0; i < 10; ++i) 
        src[i] = 1;

    memcpy(s.dest, src, 10);

    fprintf(stdout, "dest[0] -> %d \n", s.dest[0]);
    src[0] = 0;
    fprintf(stdout, "dest[0] -> %d \n", s.dest[0]);

    free(src);
    free(s.dest);

    return 0;
}

The execution give me the expected result, -> 1 and 1.

Any ideas ? Thanks

  • 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-28T07:42:07+00:00Added an answer on May 28, 2026 at 7:42 am

    If this code

    fprintf(stdout, "s.t_con_cust[0] -> %d \n", s.t_con_cust[0]);
    t_con_cust[0] = 0;
    fprintf(stdout, "s.t_con_cust[0] -> %d \n", s.t_con_cust[0]);
    

    results in

    s.t_con_cust[0] -> 1
    s.t_con_cust[0] -> 0

    then the only explanation that makes sense is that

    t_con_cust == s.t_con_cust
    

    If that condition was not true then how could assignment to t_con_cust[0] affect s.t_con_cust[0]?

    I predict that when you test the two pointers for equality you will find that they are indeed equal. And then you just need to find the part of the code that assigns the two pointers to the same value.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.