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

  • Home
  • SEARCH
  • 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 8827389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:21:34+00:00 2026-06-14T07:21:34+00:00

Sorry, another C pointer question.. I have a function func() that sorts an array,

  • 0

Sorry, another C pointer question.. I have a function func() that sorts an array, then get the largest and smallest integers. I’m trying to put them inside pointer variables in main() but the values are only correct inside the func() function. I don’t understand why 🙁

#include <stdio.h>

void func(int arr[], int *s, int *l, int n){
    int i = 1;
    for(; i < n; i++){
        int temp = arr[i];
        int n = i;
        while( n > 0 && arr[n-1] > temp){
            arr[n] = arr[n-1];
            n--;
        }
        arr[n] = temp;
    }
    l = &arr[n-1];
    s = &arr[0];\
    printf("%d %d\n",*l,*s);
}

int main(void){
    int arr[] = {1,2,9,3,58,21,4};
    int *s, *l;
    int size = 7;
    func(arr,s,l,size);
    printf("%d %d\n",*l,*s);
} 
  • 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-14T07:21:35+00:00Added an answer on June 14, 2026 at 7:21 am

    When you pass a pointer as an argument to a function in C, a copy of the pointer is made. Thus, changing the value of the pointer has no effect outside of that function. However, changing the value at the memory referenced by the pointer will take effect everywhere, as you want. In your case, you would need to do this:

    void func(int arr[], int *s, int *l, int n){
        // sorting code..
        // l = &arr[n-1]; WRONG - these change the value of the pointer,
        //s = &arr[0];\   without changing the value of the memory they reference
    
        *l = arr[n-1]; // CORRECT - changes the value at the referenced memory
        *s = arr[0];
        printf("%d %d\n",*l,*s);
    }
    

    Of course, the way you’re using the pointers in main is also incorrect; they’re uninitialized and likely to cause a segmentation fault. Since there appears to be no reason to use actual int* variables over ordinary int variables there, we can take another approach to passing them “by reference”:

    int main(void){
        int arr[] = {1,2,9,3,58,21,4};
        // int *s, *l; WRONG - we don't need pointers, we need to pass regular ints
        int s, l;
        int size = 7;
        // Get the address of our variables with the address-of (&) operator
        // This effectively creates int* variables out of our int variables
        func(arr, &s, &l,size);
        printf("%d %d\n",*l,*s);
    } 
    

    Note that the term “by reference” here is not correct in the true sense of the phrase, since you are still receiving a copy of the address associated with the variable. Most languages provide a true by-reference faculty by removing this distinction and only allowing you access to the variable and its value, with the copying somewhat out of sight of the programmer. You can think of this as being “by reference with respect to l and s inside main“, in the sense that their values can change due to the called function.

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

Sidebar

Related Questions

Sorry to post another question that is similar to the question that I posted
Sorry if my last question is very similar, but I have another query of
Sorry if this is a repost of another question; I couldn't find anything after
Sorry, another super basic ASP.NET question. this so embarrassing. I am reading the article
Sorry, but I'm new to accessing return from another domain. I have this url
Sorry, I know this question is easy, but I don't know how to get
Sorry, probably another really basic question. In my ViewScoped bean, a 'viewParam' looks like
Hey, sorry to be such a nuisance, but I have another jQuery problem. Same
I have a function that is called by main. Assume that function's name is
Sorry for another non programming question, but I'm using Quartz.NET, a scheduler for .NET

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.