Link to home
Start Free TrialLog in
Avatar of akohan
akohan

asked on

How to read a string from memory in C?

hello group,

I've two pointers which one points to beginning of a string in memory and also a 2nd pointer pointing to the end of it. Of course, using while() loop I can read it but how can I store it into a dynamic array or variable?

thanks,
ak
Avatar of akohan
akohan

ASKER



Ok I have done it this way but when I'm trying to close it by contatenating '\0' it causes segmentation error. Any idea?



range = end_addr-start_addr
char* p = malloc(range);
 
strncpy(p, res1, range);
strcat(p, '\0');        /* this causes segmentation error !*/

Open in new window

Well, depeding upon the difference between the addresses, you could store it in a a piece of memory that's the sizeof whatever the pointers point to [type], do some subtraction, ans then love he result?
Avatar of akohan

ASKER


Ok, for this issue I also did as following in snippet. Any idea if this is a safe way?

Thanks.


char* p = '\0';
p = ((char*) malloc(range));
 
strncpy(p, start_addr, range);
 
//now the string is pointed by p.

Open in new window

SOLUTION
Avatar of peetm
peetm
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of akohan

ASKER



Hi thanks for the heads up. One thing that your last line is mixed with some unicode characters and had made it hard to read. Can you please explain what it means?

regarding your question, range is the difference between starting and ending memory space string lies in.

Regards,
ak
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of akohan

ASKER


Thank you all for your advice.

Regards.