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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:14:28+00:00 2026-05-25T20:14:28+00:00

I am currently experimenting with C, and I am having a really hard time

  • 0

I am currently experimenting with C, and I am having a really hard time understanding how to use pointers in functions. I understand what is happening when I initialize a pointer then dereferencing it, however, I get a little confused when I have to use them in functions as follows:

I have three functions here, but I do not know exactly when to use which. I am still relatively new to C.

int returnSomething(int a, int b)
int returnSomething(int *ptrA, int *ptrB)
int* returnSomething(int *ptrA, int *ptrB);

edit:

Is there a major difference between the three?

  • 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-25T20:14:29+00:00Added an answer on May 25, 2026 at 8:14 pm

    You need to adapt your usage to every situation.

    The first case, you take two ints by value as parameters and return an int. Because your parameters are by value, any changes applied to them will only have function scope.

    For example:

    int returnSomething(int a, int b)
    {
        a = 0;
        b = 0;
        return 0;
    }
    
    //....
    
    int x = 3;
    int y = 4;
    returnSomething(a,b);
    // x will still be 3, y will still be 4
    

    In the second case, because you pass parameters as pointers, you will be able to change the values.

    int returnSomething(int* a, int* b)
    {
        *a = 0;
        *b = 0;
        return 0;
    }
    
    //....
    
    int x = 3;
    int y = 4;
    returnSomething(&a,&b);
    // x and y will be 0 here
    

    The third case, besides passing parameters by their pointer, you return a pointer to an int. This means inside the function you have to allocate memory and free it when you are done. I don’t recommend using this, there usually are workarounds to doing it.

    int* returnSomething(int* a, int* b)
    {
       int* x = malloc(sizeof(int));
       *x = 1;
       return x;
    }
    
    //....
    
    int x = 3;
    int y = 4;
    int* z = returnSomething(&a,&b);
    free(z);
    

    The answer is, it depends on what you want to do. If you need to change the parameters value in the method, pass by reference or by pointer. I wouldn’t recommend using the last method.

    Also, this applies because you’re dealing with POD types. If you have your own struct, it will be more efficient passing it by pointer or returning a pointer, since a new copy won’t have to be made.

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

Sidebar

Related Questions

I am currently experimenting with TFS, and I really like it. One small question,
I'm starting out with MVVM, and I'm starting to understand things. I'm currently experimenting
I'm currently experimenting with some APIs, and I'll like to know how to use
I am currently experimenting with Amazon EC2 and use standard ec2 console. The web
I'm currently experimenting with Scala and looking for best practices. I found myself having
I'm currently experimenting with build script, and since I have an ASP.net Web Part
I'm currently experimenting with EJB3 as a prestudy for a major project at work.
I am experimenting with filter_input and filter_var and I am currently trying to sanitize
I am experimenting with writing a toy compiler in ocaml. Currently, I am trying
I'm experimenting with Protocol Buffers in an existing, fairly vanilla Maven 2 project. Currently,

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.