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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:08:53+00:00 2026-05-13T12:08:53+00:00

Can I do something like this? Will this work? double *vec_subtraction (char *a, char

  • 0

Can I do something like this? Will this work?

double *vec_subtraction (char *a, char *b, int n)
{   
    double *result;
    int i;

    for(i=0; i<n; i++)
        result[i] = a[i]-b[i];

    return result;
}

and then in main:

double *vec=vec_substraction(a, b, n);
for(i=1; i<n; i++)         
    printf("%d", vec[i]);

a and b are vectors with the same number of elements, n is number of elements.

  • 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-13T12:08:53+00:00Added an answer on May 13, 2026 at 12:08 pm

    Yes you can, but you need to allocate memory for result somewhere.

    Basically, you can either allocate the memory inside vec_subtraction or outside vec_subtraction, if you allocate outside you can do this statically or dynamically.

    If you’re going to allocate inside:

    double *vec_subtraction (char *a, char *b, int n) {
        double *result = malloc(sizeof(double)*n);
        int i;
        for(i=0; i<n; i++)
            result[i] = a[i]-b[i];
    
        return result;
    }
    

    and in main:

    double *vec;
    // ...
    vec = vec_subtraction(a, b, n);
    // ...
    free(vec); 
    

    Don’t forget to free the result of the call to vec_subtraction sometime later.


    If you’re going to allocate outside you need to pass in a pointer to the memory:

    void vec_subtraction (char *a, char *b, int n, double *result) {
        int i;
        for(i=0; i<n; i++)
            result[i] = a[i]-b[i];
    }
    

    in main:

    // choose one of:
    // double *vec = malloc(sizeof(double)*n);
    // double vec[10]; // where 10= n.
    vec_subtraction(a, b, n, vec);
    
    // if you used *vec = malloc... remember to call free(vec).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 308k
  • Answers 308k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Turn on delayed expansion with setlocal enabledelayedexpansion and use set… May 13, 2026 at 9:41 pm
  • Editorial Team
    Editorial Team added an answer Sometimes (if the user doesn't have cookies enabled, for example),… May 13, 2026 at 9:41 pm
  • Editorial Team
    Editorial Team added an answer I've been grappling with this today and found that you… May 13, 2026 at 9:41 pm

Related Questions

So I have a datagridview. Normally I can create a bindinglist of my object
I am working with a generic data structure, say MyGeneric<Type> . There is a
I have a firm grasp on XHTML & CSS, but PHP & Javascript look
I have a situation where I want my program to read in some numbers
I spend much of my time programming in R or MATLAB. These languages are

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.