?
Solved

How to read a string from memory in C?

Posted on 2008-10-09
8
Medium Priority
?
214 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 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