Pointer segmentation fault error

I have something like: "object_*_amount"
 pencil * 23 

and I want to get the name and amount in different variables. So I read all the string in s, than when finds " * " replace first space with NULL. When reads again s, it stops to NULL and for p+3 to the end...
1
2
3
4
5
6
7
8
9
10
    int n, k, i, j, t, l;
    char *p, s[300], sep[]=" * ";
    in>>n;
    in.getline(s,300);
    p=strstr(s,sep);
    *p=NULL;
    t=atoi(p+3);
    k=1;
    strcpy(h[1].nume,s);
    h[1].cant=t;

But when it gives an "segmentation fault" error at *p=NULL;
Any ideeas how I can replace *p=NULL;?
Edit: Missed the strstr line. Ignore this!
Last edited on
Before assigning NULL to p you shall check whether it is equal to NULL. So instead of

1
2
    p=strstr(s,sep);
    *p=NULL;


shall be

1
2
    p=strstr(s,sep);
    if ( p ) *p=NULL;


EDIT: Also you assigned NULL to p and after you are trying to execute

t=atoi(p+3);

You may not access memory at address 3.:)
Last edited on
Still not working.
Is t=atoi(p+3); ok?
When I debug, it stops there with same error.
Read my supplement in the previous post.
If p is of type char*, then assigning *p = NULL doesn't read quite right.

It works in C++ as NULL is just 0. But it would be better to use *p = '\0'; as NULL is for a pointer, not a char (which is what *p is)

Note that a modern compiler, which understands the modern equivalent to NULL, nullptr, would not play ball.

*p = nullptr;

error C2440: '=' : cannot convert from 'nullptr' to 'char'

Andy

PS If you compiles as C using VC++ with the appropriate headers, you get

warning C4047: '=' : 'char' differs in levels of indirection from 'void *'

As NULL is ((void *)0), rather than just 0, to remind you it's for pointers.
Hi bratulenu21,

char *p, s[300], sep[]=" * ";

this is the same as :

1
2
3
4
char *p;
char s[300];
char  sep[]=" * ";  


Just checking if that is what you want, it is probably fine, but your kind of declaration sometimes leads people into the trap of thinking they are all pointers. In this case they are, only because an array name is a pointer. Be aware you cannot do pointer arithmetic with an array name, unless you send to a function, in which case it is a local copy.

I try to avoid multiple declarations per line, - it can easily lead to errors.

If you want to set p to NULL, just leave out the dereference - p=NULL;

I am not sure you need to do that though. Could you use the space char as delimiter - this will split the string into 3. I can't remember whether there is a function that will return an array with the 3 sub strings in it, or whether you just call strstr 3 times. Probably the latter for C, and the former for C++.

I found this example for C:
http://www.cplusplus.com/reference/clibrary/cstring/strpbrk/


http://www.cplusplus.com/reference/clibrary/cstring/strtok/


And this one for C++:

http://www.cplusplus.com/reference/algorithm/find/


Hope all this helps.


I have been ninja'ed multiple times!! Must learn to type faster. Hope my advice was OK.
Last edited on
Topic archived. No new replies allowed.