By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,122 Members | 1,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,122 IT Pros & Developers. It's quick & easy.

Intializing Pointer Array

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.