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

Segmentation Error in Code...

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);

}
May 25 '07 #1
6 1775
Savage
1,764 Expert 1GB
I am getting "Segmentation Fault" for the following program. How to avoid it.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #pragma align
  4. void print(char*,int);
  5.  
  6. struct data{
  7.             int a;
  8.             char b;
  9.            }
  10.  
  11. main()
  12. {
  13.  int n;
  14.  char *ptr;
  15.  struct data *p;
  16.  p->a=7;
  17.  p->b="c";
  18.  
  19.  printf("Enter n:");
  20.  scanf("%d",&n);
  21.  print(ptr,n);
  22. }
  23.  
  24. print(char *ptr1,int m)
  25. {
  26.  struct data *ptr2;
  27.  int i;
  28.  ptr2=(struct data*)ptr1;
  29.  for(i=0;i<m;i++)
  30.      printf("a=%d\tb=%c",ptr2->a,ptr2->b);
}
You need to allocate memory for struct pointer.

Are u using C or C++?

Savage
May 25 '07 #2
Silent1Mezzo
208 100+
I am getting "Segmentation Fault" for the following program. How to avoid it.
Expand|Select|Wrap|Line Numbers
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #pragma align
  5. void print(char*,int);
  6.  
  7. struct data{
  8.             int a;
  9.             char b;
  10.            }
  11.  
  12. main()
  13. {
  14.  int n;
  15.  char *ptr;
  16.  struct data *p;
  17.  p->a=7;
  18.  p->b="c";
  19.  
  20.  printf("Enter n:");
  21.  scanf("%d",&n);
  22.  print(ptr,n);
  23. }
  24.  
  25. print(char *ptr1,int m)
  26. {
  27.  struct data *ptr2;
  28.  int i;
  29.  ptr2=(struct data*)ptr1;
  30.  for(i=0;i<m;i++)
  31.      printf("a=%d\tb=%c",ptr2->a,ptr2->b);
  32.  
  33. }
Looks like a C program to me
You'll have to allocate memor for each of the data pointers.
p = malloc(size you need);
May 25 '07 #3
Savage
1,764 Expert 1GB
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
May 25 '07 #4
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.
May 28 '07 #5
gpraghuram
1,275 Expert 1GB
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
May 28 '07 #6
kky2k
34
exactly what you want to do it in your "print" function..if you come up with that answer probably everyone will help u out
May 29 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
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...
3
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...
21
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!
5
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...
2
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...
3
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...
3
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 ...
14
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...
1
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
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...

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.