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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:19:21+00:00 2026-06-18T19:19:21+00:00

I have the following C code: int main(int argc, char *argv[]) { int n

  • 0

I have the following C code:

int main(int argc, char *argv[])
{

    int n = argc - 1;
    int array[n];
    int m[n][n];

    int i = 0;
    for(i = 1; i<=n;i++)
    {    
             array[i] = atoi(argv[i]);
            printf("%d\n",array[i]);
    }   
    printf("array[4] = %d\n",array[4]);
    for(i = 1; i<=n;i++)
    {   
             m[i][i] = 0;
            printf("address of m[i][i] = %p\n",&m[i][i]);
    }   

    printf("value of array[4] =%d pointer = %p\n",array[4],&array[4]);

    for(i=1;i<=n;i++) printf("After %d\n",array[i]);

    return 0;
}

If i run with the following command: “./program 30 35 15 5 10 20 15” the output is:

30
35
15
5
10
20
25
array[4] = 5
address of m[i][i] = 0xbf93070c
address of m[i][i] = 0xbf93072c
address of m[i][i] = 0xbf93074c
address of m[i][i] = 0xbf93076c
address of m[i][i] = 0xbf93078c
address of m[i][i] = 0xbf9307ac
address of m[i][i] = 0xbf9307cc
value of array[4] =0 pointer = 0xbf9307cc
After 30
After 35
After 15
After 0
After 10
After 20
After 25

Notice how array[4] has the same pointer as m[n][n]. And i really don’t understand how this is possible. What is wrong with the code. Why does array[4] = m[n][n]?

  • 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-18T19:19:22+00:00Added an answer on June 18, 2026 at 7:19 pm

    Your loops are wrong, arrays in C are zero-based. Meaning the first element is a[0] and the last is a[N-1] where N is the size of the array.

    This:

    for(i = 1; i <= n; i++)
    

    Should be this:

    for(i = 0; i < n; i++)
    

    Otherwise you overstep the array boundries.

    As a side note, you are using VLA but didn’t specify a C99 tag. Be sure you know what you are doing.

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

Sidebar

Related Questions

I have the following code: int main(int argc, char** argv) { onelog a; std::cout
I have the following code: int main(int argc, char *argv[]) { if(strcmp(argv[1],-e)==0) { //perform
Suppose you have the following code: int main(int argc, char** argv) { Foo f;
I have the following C code: int main(int argc, char* argv[]) { CURL *curl;
I have the following code: #include <stdio.h> main(int argc, char *argv[]) { int n,st;
I have the following code: #define INPUT_FILE -i int main(int argc, char* argv[]) {
I have an issue with the following code. int main (int argc, const char
I have following code: #include <cstring> #include <boost/functional/hash.hpp> #include <iostream> int main(int argc, char
In the following code: int main(int argc,char * argv[]){ int * ptr; ptr =
I have the following code int main() { int a=6; void *p; p=&a; p++;

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.