By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,611 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

Segmentation Error in Code...

P: 2
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
Share this Question
Share on Google+
6 Replies


Savage
Expert 100+
P: 1,764
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

100+
P: 208
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
Expert 100+
P: 1,764
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

P: 2
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
Expert 100+
P: 1,275
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
P: 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

Post your reply

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