GCC - Incompatible Pointer Types


 
Thread Tools Search this Thread
Top Forums Programming GCC - Incompatible Pointer Types
# 1  
Old 12-17-2010
GCC - Incompatible Pointer Types

Hi guys,

here is my code written in C and the compiler error message.
Code:
    int i;
    int (*a)[10];
    for (i = 1;i <= 9;i++)
        a[i] = (int *)malloc(sizeof(int) * 10);

here is the error:
Code:
incompatible types when assigning to type ‘int[10]’ from type ‘int *’

I want to make a two dimensional array. I know there are better solutions but I'm practising this way. Smilie
What is the solution?

Last edited by pludi; 12-17-2010 at 04:29 AM.. Reason: Quote -> Code
# 2  
Old 12-17-2010
Code:
    int i;
    int* a[10];
    for (i = 0;i <= 9;i++)
        a[i] = malloc(sizeof(int) * 10);

This User Gave Thanks to jlliagre For This Post:
# 3  
Old 12-17-2010
Quote:
Originally Posted by majid.merkava
I want to make a two dimensional array. I know there are better solutions but I'm practising this way. Smilie
It's actually not that bad. You get something that acts like a 2D array without any hardcoded dimensions, that uses the standard [] operators directly without any classes or operator overloading. It also allows the array to be very large, where local variables might be quite limited in maximum size. It's also very simple.

Yours has a hardcoded size of 10 in one dimension, but the idea can be taken farther:

Code:
int r=10, c=10; // The size we want
int n;
int **rows=(int **)malloc(sizeof(int *) * r);
for(n=0; n<r; n++)
{
        rows[n]=malloc(sizeof(int) * c);
        memset(row[n], sizeof(int) * c, 0);
}

rows[5][7]=42;

...

// Can't forget this :D
for(n=0; n<r; n++)
{
        free(rows[n]);
}

free(rows);


Last edited by Corona688; 12-19-2010 at 12:27 PM.. Reason: minor but vital code fix (n<r, not n<c!)
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Incompatible types for returned object

I have a function from the Sedgewick's book // return the shortest path from v to s as an Iterable public Iterable<String> pathTo(String v) { Stack<String> path = new Stack<String>(); while (v != null && dist.contains(v)) { path.push(v); v =... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Programming

Incompatible data type fpos_t in C

This is from a program I wrote over in 1998 that I am trying to compile on a linux machine: void write_line (FILE *fp, int rec_no, line_rec *arec) { fpos_t woffset; woffset = (rec_no - 1) * sizeof(line_rec); fsetpos(fp,&woffset); fwrite(arec,sizeof(line_rec),1,fp); }On the line... (2 Replies)
Discussion started by: wbport
2 Replies

3. Red Hat

Incompatible Level of gcc?

I'm compiling an application someone gave me. It uses XLC on a Power7, running Red Hat (4? 5?). It compiles and links, but I get the following message for every .o and .exe... xlc_r: 1501-274 (W) An incompatible level of gcc has been specified. I've tried googling on this error, and I'll I... (2 Replies)
Discussion started by: Harper21
2 Replies

4. Programming

gcc 4.3.2 accept sys call warrning incompatible pointer type

Hi all, this warning is driving me nuts. I use -pedantic with -Wall and -Werror so this needs to be fixed. BUILD: GNU-Linux-x86 Any ideas? struct sockaddr_in server_addr; int addr_len = sizeof (server_addr); fd = accept(link->socket_fd, (struct sockaddr_in *)... (2 Replies)
Discussion started by: personificator
2 Replies

5. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies

6. Solaris

Installing gcc - recieve error message gcc : cannot execute

AIM- Install Oracle 11g on Solaris using VMWare Steps 1.Logged on as root 2.Created subfolders à /usr/local/bin & /usr/local/bin/gcc 3.Downloaded gcc & libiconv & unzipped them on my harddrive & burnt them on CD 4.Copied files from CD to /usr/local/bin/gcc 5.Terminal (root) à pkgadd -d... (8 Replies)
Discussion started by: Ackers
8 Replies

7. UNIX for Advanced & Expert Users

UNIX patches with incompatible packages

I have a Sun Sparc machine with Solaris 9 on it as oracle server. We added two patches (112233-11: SunOS 5.9:Kernel Patch and 111722-04: SunOS 5.9:MathLibrary(libm)patch). When I prepared the server for Oracle installation, I checked patch with command: $/usr/sbin/patchadd -p | grep <patch_number>.... (0 Replies)
Discussion started by: duke0001
0 Replies

8. UNIX for Advanced & Expert Users

-pg incompatible with -shared

while compiling my code with -pg option i got the following error: ld (prelink): -pg incompatible with -shared; assuming -nopg any idea to overcome this problem? how can i use gprof profiler for a program using shared libraries? (2 Replies)
Discussion started by: yakari
2 Replies

9. UNIX for Advanced & Expert Users

rpc.rstatd: incompatible...

Hi, I'm working with Mercury tools: Loadrunner, and I'm trying to monitor a unix server by rstatd, and I got next error: Mar 4 11:25:56 sacindt rpc.rstatd: incompatible to /proc. Could not read disk_io: data does any one have an idea about this.. regards (0 Replies)
Discussion started by: toto2000ff
0 Replies
Login or Register to Ask a Question