Intializing Pointer Array

T

tommyPrep

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.
 
R

Richard Heathfield

(e-mail address removed) 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.
 
F

fool

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
 
T

tommyPrep

Richard said:
(e-mail address removed) 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.
 
R

Richard Heathfield

(e-mail address removed) said:

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. :)
 
C

CBFalconer

.... 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.
 
B

Barry Schwarz

On 23 Dec 2006 10:34:09 -0800, (e-mail address removed) 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
 
R

Random832

2006-12-24 said:
On 23 Dec 2006 10:34:09 -0800, (e-mail address removed) 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.
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.
 
R

Richard Heathfield

Random832 said:
2006-12-24 said:
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.

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

Yeah 'tis.
 
J

Joe Wright

Richard said:
Random832 said:
2006-12-24 said:
On 23 Dec 2006 10:34:09 -0800, (e-mail address removed) 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.

"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?
 
B

Barry Schwarz

2006-12-24 said:
On 23 Dec 2006 10:34:09 -0800, (e-mail address removed) 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.
"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
 
R

Random832

2006-12-24 said:
Random832 said:
2006-12-24 said:
On 23 Dec 2006 10:34:09 -0800, (e-mail address removed) 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]
 

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
474,262
Messages
2,571,056
Members
48,769
Latest member
Clifft

Latest Threads

Top