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
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
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?
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you all for your advice.
Regards.
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?
Open in new window