Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to read a string from memory in C?

Posted on 2008-10-09
8
Medium Priority
?
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Author Comment

by:akohan
ID: 22683799


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
ID: 22683853
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
ID: 22683857

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Assisted Solution

by:peetm
peetm earned 360 total points
ID: 22683954
>>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
 

Author Comment

by:akohan
ID: 22684129


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 440 total points
ID: 22684296
>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 400 total points
ID: 22684598
>> 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
ID: 22689852

Thank you all for your advice.

Regards.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

618 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