473,394 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Intializing Pointer Array

The discussion is in reference with the following code :

#include <stdio.h>
#include <stdlib.h>

#define MAXCOLS 10
int main()

{

int (*a)[MAXCOLS];
int nrows,ncols,i;

scanf("%d",&nrows);
scanf("%d",&ncols);

/* allocate intial memory */

*a = (int *) malloc( sizeof(int));
/* Code continued */

}

While compiling this code it is giving error like
"Incompatible types in assingment"

This is probably the rvalue is returning int *
but the lvalue is expecting int [].

I have tried different options but some of them are compiling
with a warning in some compilers while in some it is giving error.

Can any one please let me know how to rectify this.

Dec 23 '06 #1
11 1303
to*******@gmail.com said:
The discussion is in reference with the following code :

#include <stdio.h>
#include <stdlib.h>

#define MAXCOLS 10
int main()

{

int (*a)[MAXCOLS];
a is a pointer to an array of MAXCOLS ints.
int nrows,ncols,i;

scanf("%d",&nrows);
Did this work? How do you know?
scanf("%d",&ncols);
Did that work? How do you know?
>
/* allocate intial memory */

*a = (int *) malloc( sizeof(int));
You're only allocating enough space for a single int, which probably isn't
what you want. You're making a cast the point of which escapes me entirely.
And you're trying to assign a value to the object pointed to by a which,
unfortunately, doesn't actually point at any object. And even if it did, it
would be pointing to an array object, and you can't assign to array
objects.
While compiling this code it is giving error like
"Incompatible types in assingment"
Surely not? :-)
This is probably the rvalue is returning int *
but the lvalue is expecting int [].
And you can't assign to arrays, remember.
>
I have tried different options but some of them are compiling
with a warning in some compilers while in some it is giving error.

Can any one please let me know how to rectify this.
Instead of trying to be clever, state in simple terms what you are trying to
achieve, and maybe we can come up with a better way for you to achieve it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 23 '06 #2
In article <11*********************@79g2000cws.googlegroups.c om>,
to*******@gmail.com says...
The discussion is in reference with the following code :

#include <stdio.h>
#include <stdlib.h>

#define MAXCOLS 10
int main()

{

int (*a)[MAXCOLS];
int nrows,ncols,i;

scanf("%d",&nrows);
scanf("%d",&ncols);

/* allocate intial memory */

*a = (int *) malloc( sizeof(int));
/* Code continued */

}

While compiling this code it is giving error like
"Incompatible types in assingment"

This is probably the rvalue is returning int *
but the lvalue is expecting int [].

I have tried different options but some of them are compiling
with a warning in some compilers while in some it is giving error.

Can any one please let me know how to rectify this.

http://web.torek.net/torek/c/pa.html
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
Dec 23 '06 #3

Richard Heathfield wrote:
to*******@gmail.com said:
The discussion is in reference with the following code :

#include <stdio.h>
#include <stdlib.h>

#define MAXCOLS 10
int main()

{

int (*a)[MAXCOLS];

a is a pointer to an array of MAXCOLS ints.
int nrows,ncols,i;

scanf("%d",&nrows);

Did this work? How do you know?
scanf("%d",&ncols);

Did that work? How do you know?

/* allocate intial memory */

*a = (int *) malloc( sizeof(int));

You're only allocating enough space for a single int, which probably isn't
what you want. You're making a cast the point of which escapes me entirely.
And you're trying to assign a value to the object pointed to by a which,
unfortunately, doesn't actually point at any object. And even if it did, it
would be pointing to an array object, and you can't assign to array
objects.
While compiling this code it is giving error like
"Incompatible types in assingment"

Surely not? :-)
This is probably the rvalue is returning int *
but the lvalue is expecting int [].

And you can't assign to arrays, remember.

I have tried different options but some of them are compiling
with a warning in some compilers while in some it is giving error.

Can any one please let me know how to rectify this.

Instead of trying to be clever, state in simple terms what you are trying to
achieve, and maybe we can come up with a better way for you to achieve it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
-------------------------------------------------------------------------------------------------
Yes you are right

We cann't assign to array objects.

Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));

It was trying to allocate memory for a 2D array.

Author has given the explanation as

-----------------------------------
a - | | | | | | | |
-----------------------------------
^
|

*(a)

-----------------------------------
( a + 1) - | | | | | | | |
-----------------------------------

^
|
*(a + 1)
So by the above mentioned command probably author wants to initialize
the
array and give the starting location returned by malloc to a

i.e. - *a = (int *) malloc(...)

But somehow the program doesn't look clean and clear.I will try to
catch some links related
to it and will come up with some solution.

Thanks for the information.

Dec 23 '06 #4
to*******@gmail.com said:

<snip>
>
Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));
In its current role, the book is doing you no favours. Is it perhaps heavy
enough to use as a door-stop?

If not, I suggest you remove the central part of each page, leaving a 1"
'rim' (but leave the covers intact). Then glue all the pages, and the back
cover, together (but leave the top cover free to hinge). Cover in brown
parcel paper (or Christmas gift wrap, if you have any spare).

You now have a rather nifty box which may just come in useful one day. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 23 '06 #5
to*******@gmail.com wrote:
>
.... snip ...
>
Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));

It was trying to allocate memory for a 2D array.
If that is an accurate quote it sounds as if you may have a Schildt
book, known in the trade as bullschildt. If so, burn it
immediately. Alternatively tear out the pages and mount them in
the outhouse for the appropriate use.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

Dec 24 '06 #6
On 23 Dec 2006 10:34:09 -0800, to*******@gmail.com wrote:
snip
>Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));
Since this code is guaranteed to invoke undefined behavior, don't give
us something like the code in the book. If you have a question, give
us the EXACT code in the book. It would also help if you identified
the book and page so those of us who have can confirm you are quoting
it correctly.
>
It was trying to allocate memory for a 2D array.
While the argument to malloc can be used to allocate space that can
simulate a 2D array of int, the definition of a strongly implies
something else is going on.
>
Author has given the explanation as

-----------------------------------
a - | | | | | | | |
-----------------------------------
^
|

*(a)

-----------------------------------
( a + 1) - | | | | | | | |
-----------------------------------

^
|
*(a + 1)
So by the above mentioned command probably author wants to initialize
the
array and give the starting location returned by malloc to a

i.e. - *a = (int *) malloc(...)
Any author who casts the return from malloc is suspect.
>
But somehow the program doesn't look clean and clear.I will try to
catch some links related
to it and will come up with some solution.

Thanks for the information.

Remove del for email
Dec 24 '06 #7
2006-12-24 <49********************************@4ax.com>,
Barry Schwarz wrote:
On 23 Dec 2006 10:34:09 -0800, to*******@gmail.com wrote:
snip
>>Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));

Since this code is guaranteed to invoke undefined behavior, don't give
us something like the code in the book.
It's only guaranteed to invoke undefined behavior if malloc does not
have a prototype in scope. Now, you could say that since we don't see an
#include it must not be there, but, then, we don't see the #define for
MAXCOLS or the enclosing function [and those statements are not valid at
file scope] either.
>>So by the above mentioned command probably author wants to initialize
the
array and give the starting location returned by malloc to a

i.e. - *a = (int *) malloc(...)

Any author who casts the return from malloc is suspect.
"guaranteed to invoke undefined behavior", though, it's not. UNLESS
malloc is not declared. But what was posted is _clearly_ not the whole
file, because we don't even see the enclosing function.
Dec 24 '06 #8
Random832 said:
2006-12-24 <49********************************@4ax.com>,
Barry Schwarz wrote:
>On 23 Dec 2006 10:34:09 -0800, to*******@gmail.com wrote:
>>>
Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));

Since this code is guaranteed to invoke undefined behavior, don't give
us something like the code in the book.

It's only guaranteed to invoke undefined behavior if malloc does not
have a prototype in scope.
I think you missed the fact that a is indeterminate and yet is being
dereferenced.

<snip>
"guaranteed to invoke undefined behavior", though, it's not.
Yeah 'tis.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 24 '06 #9
Richard Heathfield wrote:
Random832 said:
>2006-12-24 <49********************************@4ax.com>,
Barry Schwarz wrote:
>>On 23 Dec 2006 10:34:09 -0800, to*******@gmail.com wrote:
Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));
Since this code is guaranteed to invoke undefined behavior, don't give
us something like the code in the book.
It's only guaranteed to invoke undefined behavior if malloc does not
have a prototype in scope.

I think you missed the fact that a is indeterminate and yet is being
dereferenced.

<snip>
>"guaranteed to invoke undefined behavior", though, it's not.

Yeah 'tis.
Come on Richard, you're teasing the children again. First, nrows and
ncols are not in evidence and it stores malloc's return through an
unitialized pointer. A pretty good prescription for UB.

Assume after the definition of a above, we have

int nrows = 10;

Now we allocate our 2D array like..

a = malloc(nrows * sizeof *a);

Or did I take all the fun out of it?

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 24 '06 #10
On 24 Dec 2006 01:46:37 GMT, Random832 <ra****@random.yi.orgwrote:
>2006-12-24 <49********************************@4ax.com>,
Barry Schwarz wrote:
>On 23 Dec 2006 10:34:09 -0800, to*******@gmail.com wrote:
snip
>>>Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));

Since this code is guaranteed to invoke undefined behavior, don't give
us something like the code in the book.

It's only guaranteed to invoke undefined behavior if malloc does not
have a prototype in scope. Now, you could say that since we don't see an
#include it must not be there, but, then, we don't see the #define for
MAXCOLS or the enclosing function [and those statements are not valid at
file scope] either.
No, it is guaranteed to invoke undefined behavior because a is not
initialized (or perhaps is NULL) and therefore cannot be dereferenced.
>
>>>So by the above mentioned command probably author wants to initialize
the
array and give the starting location returned by malloc to a

i.e. - *a = (int *) malloc(...)

Any author who casts the return from malloc is suspect.

"guaranteed to invoke undefined behavior", though, it's not. UNLESS
malloc is not declared. But what was posted is _clearly_ not the whole
file, because we don't even see the enclosing function.
You already made this point. Do you disagree about not casting the
return from malloc?
Remove del for email
Dec 24 '06 #11
2006-12-24 <a_*********************@bt.com>,
Richard Heathfield wrote:
Random832 said:
>2006-12-24 <49********************************@4ax.com>,
Barry Schwarz wrote:
>>On 23 Dec 2006 10:34:09 -0800, to*******@gmail.com wrote:

Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));

Since this code is guaranteed to invoke undefined behavior, don't give
us something like the code in the book.

It's only guaranteed to invoke undefined behavior if malloc does not
have a prototype in scope.

I think you missed the fact that a is indeterminate and yet is being
dereferenced.
I misread it as int *a[MAXCOLS]

Dec 24 '06 #12

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

Similar topics

3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
204
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 =...
8
by: Martin Jřrgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
1
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
0
by: craig.conboy | last post by:
Using MC++ .Net 2.0, with the CLR Profiler (from Microsoft) I am seeing that 3 blocks of memory, each ~48MB in size, being allocated by some code that initializes a managed array. It is a jagged...
1
by: seforo | last post by:
Hi Guys, How do I intialize a two dimensionally array dynamically
5
by: seforo | last post by:
Hi Guys, What is the advantage of intializing the parameter list of the method, e.g class ReadFile { ReadFile(const char* filename = "Mystuff.txt") { // Function defination...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.