473,408 Members | 1,786 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,408 software developers and data experts.

In linux system why this?

I am not able to use pointer to array in Linux .It always shows error
when I try to read from keyboard or print to screen.
main()
{
int i,j;
int (*a)[2];
a=(int *)calloc(2,sizeof(int));
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",(*(a+i)+j));
}
This shows segmentation fault?Why is this happening
But array of pointers works.

Dec 26 '05 #1
6 1201

smartbeginner wrote:
I am not able to use pointer to array in Linux .It always shows error
when I try to read from keyboard or print to screen.
main()
{
int i,j;
int (*a)[2];
a=(int *)calloc(2,sizeof(int));
casting is redundant for calloc because it returns void* type.
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",(*(a+i)+j));
}
This shows segmentation fault?Why is this happening
Because you are accessing memory location that does
not belong to your program. You are allocating only enough memory
for 2 integers and scanning into 4 integers.
But array of pointers works.


I don't know how it worked, show us the code.

Dec 26 '05 #2

smartbeginner wrote:
I am not able to use pointer to array in Linux .It always shows error
when I try to read from keyboard or print to screen.
main()
{
int i,j;
int (*a)[2];
a=(int *)calloc(2,sizeof(int));
This cast is both wrong and unnecessary. Remove it from the statement.
Actually, it would be better just to write

a = malloc(sizeof *a);

Much simpler.
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",(*(a+i)+j));
}
This shows segmentation fault?Why is this happening
Because you are attempting to access memory outside of what you've
allocated. You have allocated an array of 2 ints, (*a)[0] and (*a)[1].
When both i and j are 1, you are trying to write to (*a)[2], which is
outside of the memory you allocated.
But array of pointers works.


Dec 26 '05 #3
smartbeginner wrote:
I am not able to use pointer to array in Linux .It always shows error
when I try to read from keyboard or print to screen.
main()
{
int i,j;
int (*a)[2];
a=(int *)calloc(2,sizeof(int));
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",(*(a+i)+j));
}
This shows segmentation fault?Why is this happening
But array of pointers works.


Your code contains lots of errors. What sources have you used when
learning C, I'm just curious?
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Dec 26 '05 #4
Hello Sir,

Can you please point out my errors.So that I can study C better.Thank
you for your speedy reply

Dec 26 '05 #5
"smartbeginner" <rr******@gmail.com> writes:
Hello Sir,

Can you please point out my errors.So that I can study C better.Thank
you for your speedy reply


Many of your errors are documented in <http://cfaj.freeshell.org/google/>.
Fix those, and then we can talk about errors in C.

--
Keith Thompson (The_Other_Keith) 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.
Dec 26 '05 #6
On 26 Dec 2005 05:06:24 -0800, "smartbeginner" <rr******@gmail.com>
wrote:
I am not able to use pointer to array in Linux .It always shows error
when I try to read from keyboard or print to screen.
main()
{
int i,j;
int (*a)[2];
a=(int *)calloc(2,sizeof(int));
If your compiler did not issue a diagnostic for this statement, you
need to correct your configuration parameters or get one that deals
with the C language. The right hand side of your statement has type
pointer to int (courtesy of the cast). The left hand side has type
pointer to array of two int (from the definition of a). There is no
implicit conversion between these types. This is a constraint
violation requiring a diagnostic.

While we recommend not casting the return from the allocation
functions, in this case your cast serves to indicate that you do not
understand the type of a.

If you remove the cast (and remember to include stdlib.h), the
statement should allocate space for one array of two int. If you had
really intended to allocate space for two arrays, you should follow
the recommended practice of
a = calloc(2, sizeof *a);
In this case, malloc may be more appropriate since you never use the
zero values that calloc stores in the allocated area.
for(i=0;i<2;i++)
for(j=0;j<2;j++)
scanf("%d",(*(a+i)+j));
Here is where you introduce undefined behavior. You should consider
yourself fortunate that the UB manifested itself in an immediately
obvious manner. This discussion group is full of queries from
confused people who were not so fortunate.

Variable a is a pointer to an object (in this case an array of two
int.)

The expression a+i involves pointer arithmetic. (Pointer
arithmetic is different than "normal" arithmetic because the size of
the object pointed to participates. If p is a pointer to an object of
size 5, then p+i evaluates to an address that is 5*i bytes greater
than the value in p.) The expression evaluates to the address of the
i-th object after the one pointed to. Note that expression a+0, or
the equivalent a, evaluates to the address of the object pointed to,
which is exactly what we mean when we say a is a pointer to an object.

*a and the equivalent *(a+0) evaluate to the object, in this case
an array of two int.

*(a+i) evaluates to the i-th object after the one pointed to by a.
Unfortunately, in this case there is no such object whenever i is
greater that 0. You only allocated space for one array.
}
This shows segmentation fault?Why is this happening
But array of pointers works.


An array of pointers can work. A pointer to an array can work. A
dynamic 2-dimensional array pointed to by an int** can work. There
are probably several more approaches that can work. All you need is
the correct code for the approach you choose.
<<Remove the del for email>>
Dec 27 '05 #7

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

Similar topics

4
by: inquirydog | last post by:
Hello- I, the inquirydog, would like to solicit suggestions for a new web page I am making: I am creating a simple website that will translate concepts between windows os's, Linux, and the...
4
by: Jhuola Hoptire | last post by:
Just installed J2RE 1.4.2 on a Linux server. I am very knew to the POSIX world. I couldn't dig-up much in the docs or via google about the following: 1 - Is there a standard way to make sure...
12
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I...
383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
0
by: Zorba.GR | last post by:
IBM DB2 Connect Enterprise Edition v8.2, other IBM DB2 (32 bit, 64 bit) (MULTiOS, Windows, Linux, Solaris), IBM iSoft Commerce Suite Server Enterprise v3.2.01, IBM Tivoli Storage Resource Manager...
30
by: Christopher Kurtis Koeber | last post by:
Here is the story from Linux.org: http://www.linux.org/news/2004/10/05/0009.html I just want to know what everyone thinks on the issue. What will all of us VB developers do, there isn't much of...
0
by: Andrew.Robinson.group.com | last post by:
Linux is a free operating system that was created by Linus Torvalds when he was a student at the University of Helsinki in 1991. Torvalds started Linux by writing a kernel - the heart of the...
1
Nepomuk
by: Nepomuk | last post by:
In most modern distributions of Linux and Unix (at least ones with graphical environments like KDE, Gnome or XFCE), you get to mount your partitions easily from the desktop. In some cases however, it...
1
Nepomuk
by: Nepomuk | last post by:
You might have heard about Linux, but you don't know what it is? Or you know a few things about it, but they terrify you? Well, then this article is for you. Don't be afraid - Everyone can use Linux!...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.