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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:57:04+00:00 2026-06-16T07:57:04+00:00

Im new with c language and im having difficult while trying to change an

  • 0

Im new with c language and im having difficult while trying to change an array on my main function using another function. Besides seeing the solution I’d also like to get a full explanation what is wrong with my code and what’s the explanation to your solution.

OK so I did too much tryouts and error experimentation but with no success to solve my problem. Eventually this is my current code:

#include <stdio.h>
#define MAX 20
typedef int Values[MAX];

int changeArr(Values *vals2) 
{
    *vals2[0] = 200;
    *vals2[1] = 100;
    printf("%d and ", *vals2[0]);
    printf("%d\n", *vals2[1]);
    return 0;
}   

int main (int argc, char *argv[]) 
{
    Values vals;
    changeArr(&vals);
    printf("%d and ", vals[0]);
    printf("%d\n", vals[1]);
    return 0;
}

Output is:

200 and 100
200 and 0

Instead of:

200 and 100
200 and 100
  • 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-16T07:57:06+00:00Added an answer on June 16, 2026 at 7:57 am

    Version 1:

    #include <stdio.h>
    #define MAX 20
    typedef int Values[MAX];
    
    int changeArr(int vals2[]) {
        vals2[0] = 200;
        vals2[1] = 100;
        printf("%d and ", vals2[0]);
        printf("%d\n", vals2[1]);
        return 0;
    }   
    
    int main (int argc, char *argv[]) {
    
        Values vals;
        changeArr(vals);
        printf("%d and ", vals[0]);
        printf("%d\n", vals[1]);
        return 0;
    
    }
    

    Instead of passing a pointer to the array, pass a pointer to the array’s first element.

    Version 2:

    #include <stdio.h>
    #define MAX 20
    typedef int Values[MAX];
    
    int changeArr(Values *vals2) {
        (*vals2)[0] = 200;
        (*vals2)[1] = 100;
        printf("%d and ", (*vals2)[0]);
        printf("%d\n", (*vals2)[1]);
        return 0;
    }   
    
    int main (int argc, char *argv[]) {
    
        Values vals;
        changeArr(&vals);
        printf("%d and ", vals[0]);
        printf("%d\n", vals[1]);
        return 0;
    
    }
    

    use parentheses to compensate for the precedence.

    The problem in your code is that

    *vals2[1]
    

    is *(vals2[1]), so it dereferences a pointer one unit of Values after the passed pointer. You have no accessible memory allocated there, so it’s undefined behaviour. In practice, it is the same as accessing

    arr[1][0]
    

    for an

    int arr[2][MAX];
    

    But your vals is only (equivalent to) an int arr[1][MAX];, so you are accessing out of bounds. But if nothing worse happens, *vals2[1] = 100; in changeArr sets vals[MAX] in main to 100. It may overwrite something crucial, though.

    In int changeArr(Values *vals2) you are passing a pointer to an array of MAX ints, resp the first such array in an array of Values. Then vals2[1] is the second array in that array of Values (which doesn’t exist here), and *vals2[1] == vals2[1][0] the first int in that second array. You want to modify elements of the first (and only) array in the pointed-to memory block, so you want to access vals2[0][1], or equivalently (*vals2)[1].

    A picture:

    vals2                          vals2[1]
     |                               |
     v                               v
    |vals[0]|vals[1]|...|vals[MAX-1]|x
    

    in changeArr, the pointer vals2 points to the array vals. Since it’s a pointer to an int[MAX], vals2+1 points to an int[MAX] at an offset of MAX*sizeof(int) bytes after the start of vals (which is just behind the end of vals). vals2[1], or equivalently *(vals2 + 1), is that array just after vals (which doesn’t exist).

    You want to change vals[1], which is located in the array vals2[0], at an offset of 1*sizeof(int) bytes, so you need vals2[0][1] or equivalently (*vals2)[1].

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

Sidebar

Related Questions

I'm trying to pick up Lisp as my new language, and I'm having some
While I'm learning a new language, I'll typically put lots of silly println's to
I'm using GNU Bison 2.4.2 to write a grammar for a new language I'm
I'm trying to teach myself C++, and one of the traditional new language exercises
I've been working on this project for a while, it's a new language for
Having a slight problem on C#, still quite new to the language but hoping
I'm really new to assembly language, and I'm trying to decode an assembly file
I'm pretty new to require.js and having problems loading i18next.js. main.js require([lib/jquery, lib/i18next, config.i18next,
Whenever I learn a new language/framework, I always make a content management system... I'm
I'm currently developing a new language for programming in a continuous environment (compare it

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.