I am just trying to explore c and i was wonder what is reason behind ... segmentation fault..
but second function is workiing .. can someone explain me reason

void myfunc1(char *t)
{
    t = t+1;
   t = 'l';   //it gives segmentation fault   ? why
 }
void myfunc2(char *s)
{
    s[2] = 'l'; /* it works fine */
 }
int main()
{
   char * x ="vipin";
   char y[] = "vipin";
   
myfunc1(x);
myfunc2(y);
printf("%s \n",x);
printf("%s \n",y);
  return 0;
}

Recommended Answers

All 11 Replies

A seg fault occurs because you are attempting to access a memory location that is not part of the memory space that you are supposed to be accessing. In other words, you have an error in your program that is causing it to end up in an unallowed memory location. You should think of this as a good thing, because it indicates that you have an error. If you didn't get a seg fault, you'd still have an error, but you wouldn't know about it (yet).

I am just trying to explore c and i was wonder what is reason behind ... segmentation fault..
but second function is workiing .. can someone explain me reason

void myfunc1(char *t)
{
    t = t+1;
   t = 'l';   //it gives segmentation fault   ? why
 }

}

You declared your function as taking a pointer to a character. t refers to the memory location where this character is stored. However, to access that memory location, you must use different syntax.

*t= 'l'; should work, if I remember correctly. Try it.

Also, think about the problem as this. If you declare it as char * t,

The contents of t is a memory address, lets say 0xEEFAA (whatever it is, not of concern)
what you WANTED to do is access that memory location and change its contents to 'l'. You can do that by saying *t = 'l';

You declared your function as taking a pointer to a character. t refers to the memory location where this character is stored. However, to access that memory location, you must use different syntax.

*t= 'l'; should work, if I remember correctly. Try it.

Also, think about the problem as this. If you declare it as char * t,

The contents of t is a memory address, lets say 0xEEFAA (whatever it is, not of concern)
what you WANTED to do is access that memory location and change its contents to 'l'. You can do that by saying *t = 'l';

Hi first of all i want to say sorry that i have paste here wrong code..
This is same what you have correcetd i am accessing memory like that *t='l';
but it gives segmentation fault .

#include<stdio.h>
void myfunc1(char *t)
{
   t = t+1;
   *t = 'l'; //it gives segmentation fault ? why
}
void myfunc2(char *s)
{
   s[2] = 'l'; /* it works fine */
}
int main()
{
   char * x ="vipin";
   char y[] = "vipin";

   myfunc1(x);
   myfunc2(y);
   printf("%s \n",x);
   printf("%s \n",y);
   return 0;
}

Like I said, t is a MEMORY LOCATION. You never changed the code where you said

t = t + 1;

That line of code looks at the memory location that is stored in t, and adds '1' to that memory location. You are getting a seg fault since this is not allowed, since the new memory location that you tried to assign to t is not allowed for your program. What you wanted to do was add '1' to the value that is at t, you didn't want to add 1 to t's memory location. To do so, dereference the pointer like this:

*t = whatever;


the '*' operator goes to the memory location that is stored in t and allows you to change its contents.

char * x ="vipin";

Now the pointer x refers to the char array {'v','i','p','i','n',0} representing the string literal "vipin". String literals are constants and placed in read-only memory pages (on all modern systems). Regrettaby, C forbid to overwrite literals but it's not a sematics error to declare non-constant pointer to a string literal (for backward compatibility). Good compilers may print warnings in that case...

char y[] = "vipin";

Now v denotes an array of 6 char elements with the same initial contents as above, but it's not a constant area, you may change elements of y.

That's why you catch exception in the 1st case and run ok in the 2nd.

char * x ="vipin";

Now the pointer x refers to the char array {'v','i','p','i','n',0} representing the string literal "vipin". String literals are constants and placed in read-only memory pages (on all modern systems). Regrettaby, C forbid to overwrite literals but it's not a sematics error to declare non-constant pointer to a string literal (for backward compatibility). Good compilers may print warnings in that case...

char y[] = "vipin";

Now v denotes an array of 6 char elements with the same initial contents as above, but it's not a constant area, you may change elements of y.
That's why you catch exception in the 1st case and run ok in the 2nd.

Thank you guys i got it !!!!!!!!!!!

I have one more question why can't we do as following in case of structures in C?

typedef struct _compare
{
int x;
char y;
char *str;
}Compare;

Compare p1,p2;

p1 = p2 /*why it is wrong in C while int a,b; a=b is valid */

And what can goes wrong if i memcopy structure p1 into p2 due to pointer variable inside structure;
What is safest way to do that?

typedef struct_compare is typedef struct compare . I guess it was just a typo.

I created a sample structure 'compare' and created two variables of type 'compare'. I initialized the members of one type and assigned it to the other using '=' operator. It seemed to work fine. Did i miss something in your question?

It was not a typo, it's struct _compare with blank between struct keyword and tag name. That's OK.
Moreover, p1 = p2 below is OK too.
What's a problem?

It was not a typo, it's struct _compare with blank between struct keyword and tag name. That's OK.
Moreover, p1 = p2 below is OK too.
What's a problem?

Oh you're right. I didn't observe it properly.

Thank you guys i got it !!!!!!!!!!!

I have one more question why can't we do as following in case of structures in C?

typedef struct _compare
{
int x;
char y;
char *str;
}Compare;

Compare p1,p2;

p1 = p2 /*why it is wrong in C while int a,b; a=b is valid */

And what can goes wrong if i memcopy structure p1 into p2 due to pointer variable inside structure;
What is safest way to do that?

Sorry Guys this question was asked coz before C90 c does not support structure copy but after that it support. so i miss that.
But still my one question is there..

what is best for to avoid performance overhead

copy like this.. p1 = p2
or memcpy(p1,p2,sizeof(p1));

or copying individual element of one structure tyo another?

if you people have performance issue in ur programming which statement would you prefer..?

Of course, p1 = p2. What's a question?
I don't think that performance problems raises with struct assignments.

I prefer simplest and clearest solutions of any code problems ;).
I prefer to solve performance problems:
1. Before coding on architecture design stage.
2. After thorough profiling, sometimes come back to #1, mostly need some refactoring...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.