Solved

How to read a string from memory in C?

Posted on 2008-10-09
8
195 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:akohan
8 Comments
 

Author Comment

by:akohan
Comment Utility


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

0
 
LVL 10

Expert Comment

by:peetm
Comment Utility
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?
0
 

Author Comment

by:akohan
Comment Utility

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

0
 
LVL 10

Assisted Solution

by:peetm
peetm earned 90 total points
Comment Utility
>>char* p = '\0';
p = ((char*) malloc(range));
 
strncpy(p, start_addr, range);
 
//now the string is pointed by p.

---

The cast on p = ((char*) malloc(range)); isn't necessary

And you should check that p, after the alloc isnt NULL

Whats range?

If all of the above works, you might check that theres the not the classic off by one  a Google test.


0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:akohan
Comment Utility


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
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 110 total points
Comment Utility
>char* p = '\0';
>p = ((char*) malloc(range));
 >strncpy(p, start_addr, range);

If your string was "ab" - what would be your range? If your end_ptr was pointing to b then it would be 1 ... If it was pointing to \0 following b then it would be 2. You need 3 chars to store this string so you may have to add 2 or 1 to your range depending on where end_ptr points

>strcat(p, '\0');
second argument of strcat has to be char * or const char * ... you are passing a char - a low value that is being treated by strcat is an address - hence the segmentation fault.

You would rather do something like *(p+n) = '\0';
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 100 total points
Comment Utility
>> strncpy(p, res1, range);
>> strcat(p, '\0');        /* this causes segmentation error !*/

Note also that you probably didn't copy a trainling '\0' character with the strncpy, so you cannot use strcat after that, since it depends on the '\0' already being there.

Second :

>> char* p = malloc(range);

You only allocated memory for 'range' characters - you need one more for the trailing '\0' character.
0
 

Author Comment

by:akohan
Comment Utility

Thank you all for your advice.

Regards.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now