pointer and array

S

sumitverma1680

recently i came across this problem in one of C magzines, and it has
kept me wondering for some time.I am posting it here as i myself could
not figure it out ! any comments are most welcome.
I have tried MVC++ for general code test.

the problem is divided in two .c files as follow :

file1.c
#include <stdio.h>
extern void fn();
int arr[10] ;
int main()
{
fn();
}


file2.c
#include <stdio.h>
extern int *arr;
void fn()
{
arr[5]=5;
printf("%d",arr[5]);

}

when the above are compiled and linked there is no error . But during
de-referncing of arr ponter in fn() , memory acesss violation occurs?
can any body explain ? thanks in advance.

--- sumit ---
 
B

Ben Pfaff

file1.c
#include <stdio.h>
extern void fn();

The "extern" keyword is unneeded here.

It would be better to declare the parameters explicitly:

void fn(void);
int arr[10] ;
int main()
{
fn();
}


file2.c
#include <stdio.h>
extern int *arr;

This declaration is incorrect. It should read:
extern int arr[10];
or
extern int arr[];
void fn()
{
arr[5]=5;
printf("%d",arr[5]);

}

when the above are compiled and linked there is no error . But during
de-referncing of arr ponter in fn() , memory acesss violation occurs?
can any body explain ? thanks in advance.

The problem is that you are lying to the compiler: arr is defined
as an array but declared as a pointer. Pointers and arrays are
not the same thing. When you lie to the compiler, it will get
its revenge (as someone smarter than me once said).
 
L

Lew Pitcher

recently i came across this problem in one of C magzines, and it has
kept me wondering for some time.I am posting it here as i myself could
not figure it out ! any comments are most welcome.
I have tried MVC++ for general code test.

the problem is divided in two .c files as follow :

file1.c
#include <stdio.h>
extern void fn();
int arr[10] ;

Here you define arr as an array of 10 integers
int main()
{
fn();

}

file2.c
#include <stdio.h>
extern int *arr;

Here you define arr as a pointer to integers
Let's make sure you understand what that means: You've said that arr
names a space which contains a pointer, which, when dereferenced, will
point to an int.
void fn()
{
arr[5]=5;

Here, you dereference the pointer, add 5 * sizeof(int) to it, and use
that pointer value as the target to store the value (5) to.

The problem is that arr is an array of int, not a pointer. Since arr
is static, it's contents are initialized to zero.

So, your code above dereferences arr, pulling what it thinks is a
pointer out of a space containing 5 integer zero values, adds (5 *
sizeof(int)) to that pointer, and then tries to access the space
pointed to by that value.
printf("%d",arr[5]);

}

when the above are compiled and linked there is no error . But during
de-referncing of arr ponter in fn() , memory acesss violation occurs?

Because *(0 + (5 * sizeof(int))) is not writable by your program.
 
K

Keith Thompson

Lew Pitcher said:
On Dec 12, 12:45 pm, (e-mail address removed) wrote: [...]
int arr[10] ;

Here you define arr as an array of 10 integers
[...]
extern int *arr;

Here you define arr as a pointer to integers

[...]

Quibble: here he *declares* arr as a pointer to int. (This is a
declaration; it's not a definition.)
 
T

Tor Rustad

Ben said:
The "extern" keyword is unneeded here.

Yes, but fn() do have external linkage.
It would be better to declare the parameters explicitly:

void fn(void);

Agreed, but I see nothing wrong with

extern void fn(void);

which at least, I do use.
 
B

Ben Pfaff

Tor Rustad said:
Agreed, but I see nothing wrong with

extern void fn(void);

which at least, I do use.

Why do you prefer to write the extern keyword explicitly?
 
J

Joe Wright

recently i came across this problem in one of C magzines, and it has
kept me wondering for some time.I am posting it here as i myself could
not figure it out ! any comments are most welcome.
I have tried MVC++ for general code test.

the problem is divided in two .c files as follow :

file1.c
#include <stdio.h>
extern void fn();
int arr[10] ;
int main()
{
fn();
}


file2.c
#include <stdio.h>
extern int *arr;
void fn()
{
arr[5]=5;
printf("%d",arr[5]);

}

when the above are compiled and linked there is no error . But during
de-referncing of arr ponter in fn() , memory acesss violation occurs?
can any body explain ? thanks in advance.

--- sumit ---

Why lie to the compiler? In file1, arr is array 10 of int and indeed has
external linkage. In file2 you declare it a pointer to int. An array is
not a pointer. A pointer is not an array.
 
R

Robert Latest

Tor said:
I have sometimes forgot to use 'static', when using 'extern', I rarely
forget.

Except that using or not using 'static' makes a difference whereas using or
not using 'extern' doesn't.

robert
 
K

Keith Thompson

recently i came across this problem in one of C magzines,

int arr[10] ;

extern int *arr;

The editors of that "C" magazine would do well to read the C FAQ, and in
particular question 6.1: <http://c-faq.com/aryptr/aryptr1.html>.

I suspect they're aware of it, or at least of the information that it
presents. The program apparently was presented as a problem; the
reader is being asked to figure out why it doesn't work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top