I am getting "Segmentation Fault" for the following program. How to avoid it.
#include<stdio.h>
#include<stdlib.h>
#pragma align
void print(char*,int);
struct data{
int a;
char b;
}
main()
{
int n;
char *ptr;
struct data *p;
p->a=7;
p->b="c";
printf("Enter n:");
scanf("%d",&n);
print(ptr,n);
}
print(char *ptr1,int m)
{
struct data *ptr2;
int i;
ptr2=(struct data*)ptr1;
for(i=0;i<m;i++)
printf("a=%d\tb=%c",ptr2->a,ptr2->b);
}
6 1775
I am getting "Segmentation Fault" for the following program. How to avoid it. - #include<stdio.h>
-
#include<stdlib.h>
-
#pragma align
-
void print(char*,int);
-
-
struct data{
-
int a;
-
char b;
-
}
-
-
main()
-
{
-
int n;
-
char *ptr;
-
struct data *p;
-
p->a=7;
-
p->b="c";
-
-
printf("Enter n:");
-
scanf("%d",&n);
-
print(ptr,n);
-
}
-
-
print(char *ptr1,int m)
-
{
-
struct data *ptr2;
-
int i;
-
ptr2=(struct data*)ptr1;
-
for(i=0;i<m;i++)
-
printf("a=%d\tb=%c",ptr2->a,ptr2->b);
}
You need to allocate memory for struct pointer.
Are u using C or C++?
Savage
I am getting "Segmentation Fault" for the following program. How to avoid it. -
-
#include<stdio.h>
-
#include<stdlib.h>
-
#pragma align
-
void print(char*,int);
-
-
struct data{
-
int a;
-
char b;
-
}
-
-
main()
-
{
-
int n;
-
char *ptr;
-
struct data *p;
-
p->a=7;
-
p->b="c";
-
-
printf("Enter n:");
-
scanf("%d",&n);
-
print(ptr,n);
-
}
-
-
print(char *ptr1,int m)
-
{
-
struct data *ptr2;
-
int i;
-
ptr2=(struct data*)ptr1;
-
for(i=0;i<m;i++)
-
printf("a=%d\tb=%c",ptr2->a,ptr2->b);
-
-
}
Looks like a C program to me
You'll have to allocate memor for each of the data pointers.
p = malloc(size you need);
Looks like a C program to me
You'll have to allocate memor for each of the data pointers.
p = malloc(size you need);
Yes it does looks like a C program,but u can never know,maybe OP is using printf and scanf in c++.
Savage
Yes it does looks like a C program,but u can never know,maybe OP is using printf and scanf in c++.
Savage
Hi,
Could you please write the complete C code? I am an infant in C programming. I tried allocating memory for the structure pointers, i declared. But still it is showing segmentation fault.
Please help.
Hi,
Allocate memory like this
struct data *p=(struct data*)malloc(sizeof(struct data));
and init the char value with single quotes
p->b='c';
Then in print you are simply typecasting and printing the structure, but the char*ptr is not initialized properly due to which also it may crash
Check it
Raghu
exactly what you want to do it in your "print" function..if you come up with that answer probably everyone will help u out
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Vishal Grover |
last post by:
Hello Everyone,
I am seeing a certain behaviour which I find strange, and am curious to get an
explanation to it. I have the following program.
#include <iostream>
#include <cstdlib>
using...
|
by: Anks |
last post by:
i am unable to find why following code is giving segmentation
fault.... way to produce seg fault: run the program... give input
12345678....enter any key except 'x'.... again give 12345678 as...
|
by: user |
last post by:
I just finish writing LAB2 with no errors when compiling, but once i run
it, i get "segmentation fault".. i don't know what is wrong, can anyoen
tell me what it means and how i can fix it? thx!
|
by: Fra-it |
last post by:
Hi everybody,
I'm trying to make the following code running properly, but I can't get
rid of the "SEGMENTATION FAULT" error message when executing.
Reading some messages posted earlier, I...
|
by: zl2k |
last post by:
hi, all
I am using a 2 dimensioanl array implemented by vector<vector<long> >.
When the row number grows to 8 and I am trying to insert a new row, I
got the segmentation error. However, if I...
|
by: madunix |
last post by:
My Server is suffering bad lag (High Utlization)
I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2
Web visitors retrieve data from the web by php calls through oci
cobnnection...
|
by: Dhieraj |
last post by:
While compiling a C++ code I am getting the following error :
CC -c -I/opt/iona/artix/2.0/include -I/opt/iona/asp/6.0/include -I/opt/ar/api63/include -I//var/tmp/vidya/aotscommon/include ...
|
by: cnixuser |
last post by:
I am new to C programming and am still at an early level in java and C#. I am posting regarding a segmentation fault error code I get when I try to run a program that I am developing. I am coding on...
|
by: Alvin |
last post by:
Note: Source code and compile commands are at the bottom.
I've spent some time with a friend making a nice and simple to use
OOPified C++ networking layer for TCP that works on multiple...
|
by: ollii |
last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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: 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: 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,...
|
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...
| |