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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:28:15+00:00 2026-05-23T07:28:15+00:00

File 1.c int a[10]; File main.c: extern int *a; int main() { printf(%d\n, a[0]);

  • 0

File 1.c

int a[10];

File main.c:

extern int *a;

int main()
{
    printf("%d\n", a[0]);
    return 0;
}

Gives me a segfault! What’s going wrong?

  • 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-23T07:28:15+00:00Added an answer on May 23, 2026 at 7:28 am

    Arrays decompose, or are implicitly converted to pointers when passed to a function as an argument, or when converted to an r-value on the right-hand-side of the assignment operator. So something like:

    int array[10];
    int* a = array;  //implicit conversion to pointer to type int
    
    void function(int* a);
    function(array);  //implicit conversion to pointer to type int
    

    works just fine. But that does not mean that arrays themselves are pointers. So if you treat an array like a pointer as you’ve done, you’re actually treating the array type as-if it was a pointer that held the address to an int object. Since your array is actually a sequence of int objects, and not pointers to int objects, you’re actually trying to dereference to some memory location that isn’t pointing to anywhere valid (i.e., the first slot in array is a numerical integer value like 0 which would be like dereferencing a NULL). So that is why you’re segfaulting. Note that if you had done something like this:

    int array[] = { 1, 2, 3, 4, 5};
    int b = *array;
    

    That still works, since array is again implicitly converted to a pointer to the memory block that is holding a sequence of integer values and is then dereferenced to get the value in the first sequence. But in your case, by declaring your array to the current code module as an externally defined pointer, and not an array, it will skip over the implicit conversion to a pointer that is normally done, and just use the array object as-if it were a pointer to an object itself, not an array of objects.

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

Sidebar

Related Questions

Using the following code to write to a file int main(int argc, char *
int main() { char* NamePointer = new char; std::cout << Enter the file you
I have a code that looks like this: int main () { fstream file;
I have three files, a main .cpp file: #include <stdio.h> #include myClass.h int main()
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
`a.h typedef struct a { File* fp; int var; }; extern a *pp; ---------------------
file a.cc int a = 0; file b.cc #include a.cc file main.cc #include b.cc
I have a library I created, File mylib.c: #include <mylib.h> int testlib() { printf("Hello,
File 1: static char* const path; //GLOBAL int main() { path = FunctionReturningPath(); UsePath()

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.