473,598 Members | 3,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Pointer problem

Hi,

I can't understand why this code causes a "memory read exception" at
int x=**a;

void pass(int** a)
{
int x=**a;
}
void main()
{
int arr[2][2]={{1,2},{3,4}};
pass(arr);
}
The assignment of int int x=**a in the main function works.

Tanks for your help,
Markus

May 31 '06 #1
73 3796
Markus said:
Hi,

I can't understand why this code causes a "memory read exception" at
int x=**a;

void pass(int** a)
{
int x=**a;
}
void main()
main returns int.

If you can't even get the entry point right, what chance do you stand with
pointers?
{
int arr[2][2]={{1,2},{3,4}};
pass(arr);


pass() takes int **.

arr's value is taken as the address of its first element. Its first element
is an int[2] array, so the address of its first element has type int
(*)[2], which is not the same as int **.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 31 '06 #2
Markus wrote:
Hi,

I can't understand why this code causes a "memory read exception" at
int x=**a;

void pass(int** a)
{
int x=**a;
}
void main()
{
int arr[2][2]={{1,2},{3,4}};
pass(arr);
}
The assignment of int int x=**a in the main function works.


Did this really compile without any warnings or errors?

Brian
May 31 '06 #3
Default User said:
Markus wrote:
Hi,

I can't understand why this code causes a "memory read exception" at
int x=**a;

void pass(int** a)
{
int x=**a;
}
void main()
{
int arr[2][2]={{1,2},{3,4}};
pass(arr);
}
The assignment of int int x=**a in the main function works.


Did this really compile without any warnings or errors?


No.

foo.c:2: warning: no previous prototype for `pass'
foo.c: In function `pass':
foo.c:3: warning: unused variable `x'
foo.c: At top level:
foo.c:6: warning: function declaration isn't a prototype
foo.c:6: warning: return type of `main' is not `int'
foo.c: In function `main':
foo.c:8: warning: passing arg 1 of `pass' from incompatible pointer type

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
May 31 '06 #4
Richard Heathfield a écrit :
foo.c:2: warning: no previous prototype for `pass'
foo.c: In function `pass':
foo.c:3: warning: unused variable `x'
foo.c: At top level:
foo.c:6: warning: function declaration isn't a prototype
foo.c:6: warning: return type of `main' is not `int'
foo.c: In function `main':
foo.c:8: warning: passing arg 1 of `pass' from incompatible pointer type


lcc-win32 has:
D:\lcc\mc66\tes t>lcc -A tw2.c
Warning tw2.c: 3 x is assigned a value that is never used
Warning tw2.c: 6 old-style function definition for 'main'
Warning tw2.c: 6 missing prototype for 'main'
Warning tw2.c: 6 'void main()' is a non-ANSI definition
Warning tw2.c: 8 assignment of pointer to array 2 of int to pointer to
pointer to int
0 errors, 5 warnings

Basically all warnings are the same, with wording differences.
The first warning of gcc however, is not clear to me:

foo.c:2: warning: no previous prototype for `pass'

Why is that an eror?

The function definition is correct, and it wasn't
previously used. That looks correct to me.
May 31 '06 #5
Richard Heathfield wrote:
Default User said:
Markus wrote:
The assignment of int int x=**a in the main function works.


Did this really compile without any warnings or errors?


No.


Of course. It was a nudge to the OP to let him know that the compiler
doesn't issue diagnostics (just) because it doesn't like you.


Brian
May 31 '06 #6
2006-05-31 <44************ ***********@new s.wanadoo.fr>, jacob navia wrote:
Richard Heathfield a écrit :
foo.c:2: warning: no previous prototype for `pass'
foo.c: In function `pass':
foo.c:3: warning: unused variable `x'
foo.c: At top level:
foo.c:6: warning: function declaration isn't a prototype
foo.c:6: warning: return type of `main' is not `int'
foo.c: In function `main':
foo.c:8: warning: passing arg 1 of `pass' from incompatible pointer type

lcc-win32 has:
D:\lcc\mc66\tes t>lcc -A tw2.c
Warning tw2.c: 3 x is assigned a value that is never used
Warning tw2.c: 6 old-style function definition for 'main'


While void main() is a lot of things (none of them good), it is not an
old-style definition. Warning on empty brackets might make sense for
a declaration without a definition, it does NOT make sense for
a definition.
Warning tw2.c: 6 missing prototype for 'main'
Warning tw2.c: 6 'void main()' is a non-ANSI definition
Warning tw2.c: 8 assignment of pointer to array 2 of int to pointer to
pointer to int
0 errors, 5 warnings

Basically all warnings are the same, with wording differences.
The first warning of gcc however, is not clear to me:

foo.c:2: warning: no previous prototype for `pass'

Why is that an eror?

The function definition is correct, and it wasn't
previously used. That looks correct to me.

May 31 '06 #7
Jordan Abel wrote:
2006-05-31 <44************ ***********@new s.wanadoo.fr>, jacob navia wrote:

lcc-win32 has:
D:\lcc\mc66\tes t>lcc -A tw2.c
Warning tw2.c: 3 x is assigned a value that is never used
Warning tw2.c: 6 old-style function definition for 'main'
While void main() is a lot of things (none of them good), it is not an
old-style definition.


int main() is part of an an old style definition, or at least a
definition
that's been in style for a long time. ;)
Warning on empty brackets might make sense for
a declaration without a definition, it does NOT make sense for
a definition.


Compare...

int foo() { }

int main()
{
foo(42); /* no diagnostic required */
return 0;
}

With...

int foo(void) { }

int main()
{
foo(42); /* diagnostic required */
return 0;
}

--
Peter

May 31 '06 #8
On 31 May 2006 09:42:51 -0700, "Markus" <ma***@punjabi. net> wrote:
Hi,

I can't understand why this code causes a "memory read exception" at
int x=**a;
Because you lied to the compiler. Arrays are not pointers and
pointers are not arrays.

void pass(int** a)
{
int x=**a;
a is a pointer to pointer to int. To do determine the value of the
int itself, the following steps are required:

1 - Determine the value of a. It was passed "by value" to the
function using whatever calling convention is appropriate for your
system so this pretty straight forward.

2 - Use this value as the address of a pointer to int.

3 - Determine the value of this pointer.

4 - Use this value as the address of the int.

5 - Determine the value of the int.

6 - Store this value in x. (Not part of determining the value
but the concluding step in the initialization. )
}
void main()
int main(void) please.{
int arr[2][2]={{1,2},{3,4}};
pass(arr);
Since this statement has a syntax error and will not compile cleanly,
why did you bother to execute the code at all. If you did not see the
mandatory diagnostic, you need to up the warning level on your
compiler.

You pass a 2D array to the function. In this context, the array name
evaluates to the address of the first element with type pointer to
first element. In other words, this is identical to coding
pass(&arr[0]);

arr[0] is itself an array of 2 int. A pointer to arr[0] has type
pointer to array of 2 int, written as int (*)[2].

The syntax error is because an int(*)[2] is incompatible with an
int**. There is no implicit conversion between the two.

Apparently on your system the two pointer types have similar
representations (not uncommon). pass will take the value and treat it
as the address of a pointer to int (steps 1 and 2 above).

Undefined behavior #1. Since the address is actually the
address of arr[0] which is an array of 2 int, it is really the address
of arr[0][0], a normal int. You have no idea if the alignment of this
int is suitable for a pointer to int.

Undefined behavior #2. pass will attempt to use the value at
this location as the address of an int (steps 3 and 4 above). You
have no idea if the value of the int that is actually at that location
is a valid address. It could be an invalid address or even a trap
representation.

Undefined behavior #3. pass will attempt to extract the int
value at this "address" (step 5 above). Since the value at this new
address was an int and not an address to begin with, attempting to
dereference it should be an obvious no-no. This is probably where
your system decided enough was enough.
}
The assignment of int int x=**a in the main function works.
There is no a in main. Perhaps you meant
int x = **arr;

This would work because the compiler know arr is a 2D array. *arr is
identical to arr[0] which is a1D array. **arr is identical to *arr[0]
which is identical to arr[0][0] which is the first int in the 1D array
and a perfectly valid value to assign to the int x.

Tanks for your help,
Markus

Remove del for email
Jun 1 '06 #9
2006-05-31 <11************ *********@c74g2 000cwc.googlegr oups.com>, Peter Nilsson wrote:
Jordan Abel wrote:
2006-05-31 <44************ ***********@new s.wanadoo.fr>, jacob navia wrote:
>
> lcc-win32 has:
> D:\lcc\mc66\tes t>lcc -A tw2.c
> Warning tw2.c: 3 x is assigned a value that is never used
> Warning tw2.c: 6 old-style function definition for 'main'


While void main() is a lot of things (none of them good), it is not an
old-style definition.


int main() is part of an an old style definition, or at least a
definition
that's been in style for a long time. ;)
Warning on empty brackets might make sense for
a declaration without a definition, it does NOT make sense for
a definition.


Compare...

int foo() { }

int main()
{
foo(42); /* no diagnostic required */


False. The standard makes a very clear distinction between empty
brackets in a declaration not part of a definition, and empty brackets
that _are_ in part of a definition. And the latter is EXACTLY equivalent
to (void).
Jun 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2130
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived classes
5
6045
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user hits the right and left keys to move this insertion point (cursor) Here is the problem:
7
5175
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
10
4102
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr; };
204
12975
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
7
2040
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this method creates a new object (a Matrix) for it. I suppose that after the new operator, my pointer is pointing to an object, so when the method has finished, the very first pointer is still poitint to the created method; however this is not working,...
51
10299
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
2
2830
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I think it is better to have a new topic rather than continuing on old one. As now I am sure pointer to reference and reference to pointer are freely convertable, the potential danger lies in the first one. Pointer may be NULL, but reference should...
9
3007
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value accordingly ? I have this doubt as different pointers may have different representation and different size. So, how does printf determine
6
2509
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use pointer version, else if only use object for a limited scope, then use non-pointer version. Does limited scope means the object is only used in the same function
0
7899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8392
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
6718
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2412
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.