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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:38:31+00:00 2026-06-18T05:38:31+00:00

I have two pointers, each pointing to a different array. One of the pointers

  • 0

I have two pointers, each pointing to a different array. One of the pointers is inside of a struct, as follows:

typedef struct
{
    int     N;    /* Number of Elements in array */
    double *c;    /* Pointer to an array         */
    /* Other members....  */
} CS;

The struct was initialized by:

CS->N = n; /* n is an integer of an initially unknown size */
CS->c = (double *)malloc(n * sizeof(double));

The pointer in the struct, CS->C, contains data that I no longer care about.

My other pointer was defined as follows:

double *alpha;
alpha = (double *)malloc(CS->N * sizeof(double));

I need to replace the contents of CS->C with alpha. I know I can do something naive like:

for (i=0;i<CS->N;i++) /* i is an integer */
    CS->c[i] = alpha[i];

I could also use memcpy, like this:

memcpy(CS->c,alpha,CS->N * sizeof(double));

My understanding is that both of these methods will copy the contents from memory located at alpha to the memory occupied by CS->C. That being a very expensive operation, it would make more sense to simply change the assignment of CS->C to alpha.

How can I do this?

I’ve tried to reassign the pointer by doing like CS->C = &alpha, but this gives me the following warning “assignment from incompatible pointer type”.

Note: This is using ANSI C89 under full compliance, i.e. the compiler options are: -Wall -pedantic -ansi

Edit 1
Freeing CS->c and assigning it to alpha by doing:

free(CS->c);
CS->c = alpha;

does not work. It causes every entry in CS->c to become equal to 0.0 and it results in my program seg faulting.

Edit 2
I think I realized why the method suggested in my first edit did not work. alpha is a temporary pointer, created and initialized inside of a function, so once that function is exited, the memory occupied by alpha is “freed”. Since CS->c points to that memory, it is also freed. Upon this discovery, I think I will rework my code, such that alpha and CS-c are initially swapped, such that when they are switched again, the end order will be correct. Thank you all for you valuable input.

  • 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-18T05:38:32+00:00Added an answer on June 18, 2026 at 5:38 am

    Free the old pointer and assing the new directly. (Not the adress of the new pointer)

    The pointer in the struct, CS->c, contains data that I no longer
    care about.

    My other pointer was defined as follows:

    double *alpha;
    alpha = malloc(CS->N * sizeof(double));
    

    …

    free (CS->c);
    CS->c = alpha ; alpha=NULL;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of structs. The struct has two function pointers. Each element
I would like to have an array of function pointers, each pointing to a
I am considering whether it is better to have two pointers, one for each
Assume I have a structure with two pointers each pointing to an object that
I have two pointers, char *str1; int *str2; If I look at the size
I have an array of char pointers of length 175,000. Each pointer points to
I have a heap (implemented like a binary tree: each node has two pointers
I have to make a horizontal month slider which will have two pointers which
I have two unmanaged pointers in the form of IntPtr and want to copy
I have a class A containing two pointers to objects of another class B.

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.