- #include<stdio.h>
-
main()
-
{
-
int *a=5;
-
int **p=&a;
-
fflush(stdin);
-
printf("%u,%u,%d",p,*p,**p);
-
}
getting compiled perfectly but am getting
"segmentation fault" in runtime
please help
thanks a lot for the reply
i have a doubt
in the above program in the statement
int *a=5
does that symbolizes implicitly that a value is holding the memory location of 5?
i dint understand what is invalid about that......as am a newbie in c programming pointers concept
please help
4 28934 Banfa 9,065
Expert Mod 8TB
The problem is that the pointer a does not point anywhere valid.
You say it compiles perfectly, do you get no compiler warnings? If not you probably don't have you compiler warning level set high enough.
The reason I say this is that on line 4 you assign an int to an int *. This effectively makes a point at memory location 5, almost certainly not a valid location on your system.
p points to a
So when you try to print
p is valid, it is the address of a
*p is valid it is a, the address of an int
**p is not valid it is the thing that a points to, that is memory location 5, not the address of an allocate int and you get a segmentation fault (a memory address fault).
You can not take the address of a numerical constant like 5.
thanks a lot for the reply
i have a doubt
in the above program in the statement
int *a=5
does that symbolizes implicitly that a value is holding the memory location of 5?
i dint understand what is invalid about that......as am a newbie in c programming pointers concept
please help
Banfa 9,065
Expert Mod 8TB
int *a=5;
does explicitly set the value of a to (the memory location) 5. There is nothing wrong in that apart from being a little strange.
The problem comes when you try to dereference a with *a, you try to access the memory location contained in a, in this case 5 and that is not a valid memory location, or a least one you do not have access rights to.
Your code has a point to whatever int is stored at address 0x0005. The segmentation fault occurs because the operating system has not granted your program permission to access this address. In addition, you might get a run-time alignment fault if your program runs on one of the many processors that don't allow ints to be at odd addresses.
Do you intend for a to point to an int whose value is 5? If so, then you want something like this: - int five = 5;
-
int *a = &five;
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kevin Grigorenko |
last post by:
Hello all,
Okay, I've got a templated class that that takes an int and a char *, but
when I try to instantiate an object of that template, VS.NET complains with:
error C2975: 'S' : invalid...
|
by: CoolPint |
last post by:
As a self-exercise, I am trying to write a generic Priority Queue,
which would store any type and and accept any user-definable
"priority" function.
After much tinkering, I came up with...
|
by: Lucas Machado |
last post by:
i'm doing some Linux Kernel hacking for a course i'm currently taking.
there is a pointer to a struct (struct example_struct *ex_ptr) in a .c
that i want to access in a system call. i defined a...
|
by: newbie29 |
last post by:
I get this error "invalid type argument of '->'".
It occurs when:
if (num_process->t_pid == -1) {
....
}
I have defined the struct num_process in the same file as where the
above is...
|
by: bob |
last post by:
I have little C experience and am concurrently trying to tackle C and
LKM's (a little too ambitious maybe) anyway here is the problem I'm
having with an example module I found.
static int...
|
by: Suyash Upadhyay |
last post by:
Hello all,
I was writing a code regarding offset of structure elements.
struct a
{
struct b
{
int i;
float f;
char ch;
}x;
|
by: Sheldon |
last post by:
Hi Everyone,
I have defined a function:
struct Transient arrFromHdfNode(HL_NodeList *nodelist, struct
Transient retv);
and in the code:
struct Transient arrFromHdfNode(HL_NodeList...
|
by: Barry |
last post by:
The problem brought by one earlier post from comp.lang.c++
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/bf636c48b84957b/
I take part of the question and reproduce the code to...
|
by: Stephanie Smith |
last post by:
I always get this error message!
Here's the code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *string;
char length_string = 0;
char buffer_for_string = 0;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
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...
|
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...
|
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,...
| |