473,769 Members | 6,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new c programmer question

Hi, my name is John and this is my first posting to comp.lang.c. I am
learning C. I am a high school student with interest in programming. I
have a copy of "The C programming language second edition by Brian W
Kernighan and Dennis M Ritchie". I am using gcc version 4.1 on SuSE 9.

Here is my simple question. I want to set a matrix to contain some
values in my program. I want to pass in the matrix array by address in
a function. The compiler is saying:

hello.c:15: warning: assignment from incompatible pointer type

I can see what is wrong but do not have the ability yet to fix it. Here
is the small code where the error is. I would appreciate some advice on
the syntax to use.

Thank you
John

void setupmatrix(int *m[])
{
int x[4][4]=
{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12},
{13,14,15,16}
};
*m=x;
}

Sep 3 '06
14 1929
Frederick Gotham posted:
system("PAUSE") ;

Sorry, I meant to remove that before posting.

--

Frederick Gotham
Sep 3 '06 #11
John Gerrard wrote:
Okay thank you for your prompt replies everyone. I had forgotton how
painfully flow learning a new language can be (and humbleing).

I am now compiling my code with

gcc hello.c -ansi -std=c99 -pedantic -Wall -o hello
<OT>
Add a -O which will allow it to detect some other warnings. In this case
it does not show anything, but sometimes it will.
</OT>
I have modified the code. It now compiles compiles but gives this
output at runtime.

jad@dhome2:~/matrix$ ./hello
setupmatrix
exit setupmatrix
printmatrix

Segmentation fault
<OT>
Since you are using Linux you might want to try valgrind. It can help a
lot with problems like this.
</OT>
jad@dhome2:~/matrix$
jad@dhome2:~/matrix$
I have included the COMPLETE program at the end of the posting.
Thank you. This makes it a lot easier you show you what you are doing wrong.
It
doesn't work but it does represent what I am trying to do. I sort of
know what is wrong but don't have the understanding to correct it yet.
This is what I want to do: 1) allocate memory for a n*n 2D array,
OK, you need to read question 6.16 of the comp.lang.c FAQ at
http://c-faq.com/ since it specifically addresses this problem. It
describes in a lot of detail, including some pictures, three methods of
achieving this.
2)
fill it with numbers, 3) print it out, 4) and free up the memory at the
end of it all. I want to allocate the array size at runtime as I will
be, next month at this rate, be multiplying 2-3 matrices together.
Although I would like to say I don't want you to do this for me, I'm
really hoping you don't take that too seriously because I need
something working to see where I should be going.

Thank you all for your help in advance.
John

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

/*setup a n*n matrix and fill it with 1, 2, 3, 4*/
/*============== =============== =============== ==*/
int** setupmatrix(int nSize)
{
printf("setupma trix\r\n");
Why the \r? Doing a \n on its own will take you to the start of the next
line.
int nByNArraySize=n Size*nSize*size of(int);
int *arr=malloc(nBy NArraySize);
for (int i=0;i<nByNArray Size;i++)
nByNArraySize is the number of *bytes* allocated, *not* how many ints
you have space for. It would be closer (but still not a 2D array) if you
did:
int nByNArraySize=n Size*nSize;
int *arr=malloc(nBy NArraySize * sizeof *arr);
for (int i=0;i<nByNArray Size;i++)

Note the way sizeof is being used in the malloc call. No need to specify
the type there, just that it is what arr points to.
arr[i]=0;
printf("exit setupmatrix\r\n ");
return (int **)arr;
Any time you are using a cast, your first thought should be that you are
doing something wrong. There are a few occasions where a cast is needed,
but far more situations where it is harmful because it stops the
compiler from telling you that you have an error. In this case you do
have a serious error and the cast just tells the compiler not to point
it out to you.

int** means pointer to pointer. I.e. if you have:
int **arr2d;
Then arr2d[0] is a *pointer* to a row. You have not set this to point
anywhere instead you have written the integer value 0. See the FAQ
question I pointed out for the correct ways to build your int** allocation.
}
<snip>

If you have problems understanding question 6.16 then tell us what you
don't understand and we will try to help you.

I also suggest you rest of section 6 and the other question I pointed
out in my previous post.
--
Flash Gordon.
Sep 3 '06 #12
On Sun, 03 Sep 2006 18:11:30 GMT, Frederick Gotham
<fg*******@SPAM .comwrote:
>John Gerrard posted:
>void setupmatrix(int *m[])
{
int x[4][4]=
{
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12},
{13,14,15,16}
};
*m=x;
}


You can't assign one array to another (even if their dimensions are
identical), e.g.:
Since m is an array of pointers (actually it's an int** due the way
arrays are passed to functions), *m is not an array and your
statement, while correct, is not related to this code. *m has type
int* while x in this expression evaluates to type int (*)[4], pointer
to array of four int. The two types are incompatible and there is no
implicit conversion between them. That is the syntax error in the
statement.
Remove del for email
Sep 4 '06 #13
"osmium" <r1********@com cast.netwrites:
[...]
auto is a "storage class specifier" and if you do not provide one for a
variable, it will implicitly be treated as auto.
.... if and only if the variable is declared within a function
definition.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 5 '06 #14
lovecreatesbea. ..@gmail.com schrieb:
Michael Mair wrote:
> void setupmatrix(int *m)
or
void setupmatrix(int (*m)[4])

<snip>
> static int x[4][4]

<snip>
>>or even using
memcpy(&m[0], &x[0][0], sizeof x);
or
memcpy(&m[0][0], &x[0][0], sizeof x);

Will the second memcpy fail if the argument to 'void setupmatrix(int
(*m)[4])' is 'int (*m1)[4];'? Because the structures of m and x are
different. The elements in x are stored continuously while the elements
in m are not. And I think it works if the argument is 'int m2[4][4]'.
Am I right?
No.
With "int (*m1)[4]", you have a (hopefully valid) pointer to the
first of an yet unknown number of "arrays 4 of int" which are stored
contiguously, i.e. in memory you can expect
|(*m1)[0]|....|(*m1)[3]|(*(m1+1))[0]|....
or
|m1[0][0]|....|m1[0][3]|m1[1][0]|....

What you probably mean is "int **m1" or even an array of pointers
to arrays 4 of int, i.e. "int (**m1)[4]" or "int (*m1[4])[4]".
In these cases, m1[0] (or (*m1[0])) not necessarily points to
the same object as m1[1] (or (*m1[1])).

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 12 '06 #15

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

Similar topics

45
16920
by: Market Mutant | last post by:
I just wonder job selections, job openings and salary level of PHP programer or Perl programmer comparing to Java programmers. Is Java programmer's salary has a minimal of 60K in US? Are there many PHP jobs?
14
2688
by: Daniel Chartier | last post by:
Hello. I work in the paper industry and we recently had someone (the original author) from within the company make a program for preventive maintenance. However, it had some bugs and we wanted to add stuff to it, bu tthe original author/programmer was leaving, so we called in a free agent programmer. The free agent spoke with the original programmer and myself for a day. He fixed afew bugs. For the other bugs and the many...
15
10552
by: Randall Smith | last post by:
I've been programming in Python for about 2 years. I think it offers the best combination of simplicity and power of any language I have explored. As I write more and larger and complex programs, I need to code better. By better I mean clearer, cleaner, more efficient and maintainable. As the subject states, I want to become a really good Python programmer. I learn most everything from random examples and experience and that's good,...
12
1885
by: Computer Whizz | last post by:
Hiya guys, I saw this (C++ Programmer's Reference by Herbert Schildt) book in the library today, and wondered what the experienced programmer thought about it before I decided to dive in and get it. Most of the posts I've read seem to have an opinion of a wide variety of books, yet I did a google search of this group and didn't find much - maybe the odd mention of the author.
29
5358
by: jeffc | last post by:
How would you answer a question like this in an interview? I'm not interested in gathering the average of folks on this forum. I'm interested in a strategy for a) evaluating yourself objectively b) actually answering the question (which might or might not involve the anser you came up with in a)
5
2728
by: jrefactors | last post by:
when people say unix programmer, does it mean they write programs in unix environment,and those programs are run in unix platform? it is not necessary they are using unix function calls? I heard most of the time unix programmers are C and C++ programmers. please advise. thanks!!
72
5893
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
23
2709
by: Steve Jorgensen | last post by:
Hi all, I'm working on a project through a consulting company, and I'm writing some database code for use in another programmer's project in Excel/VBA. The other programmer is working through the same consulting company. I did not initially know this other programmer's experience level, but he seemed down to earth and friendly. I saw some signs of trouble after having him try to integrate some of my code, but chalked it up to him...
13
2507
by: BK | last post by:
Our .Net team has just inherited a junior programmer that we need to get up to speed as quickly as possible. Unfortunately, his skill set is largely Access with some VB6 and ASP classic experience. We employ some parts of XP such as pair programming, and this should help. Other than books, does anyone have any suggestions? His skill set is pretty antiquated and we need to get him up to speed as quickly as possible so any suggestions...
65
5286
by: Chris Carlen | last post by:
Hi: From what I've read of OOP, I don't get it. I have also found some articles profoundly critical of OOP. I tend to relate to these articles. However, those articles were no more objective than the descriptions of OOP I've read in making a case. Ie., what objective data/studies/research indicates that a particular problem can be solved more quickly by the programmer, or that the solution is more efficient in execution time/memory...
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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
10045
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6673
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
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3959
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
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.