copy long strings in C

P

Patrick

Hello

I have the following "easy" problem. I have a string which contains
1000 chars.
Now my task is to cut the string at his 650 position. I tried strcpy,
to copy the first 650 chars into another string but I got a memory
access error. I have heard that i can use this function only to copy
500 chars is that true? What other options do i have?

Best Regards
Patrick
 
W

Walter Roberson

I have the following "easy" problem. I have a string which contains
1000 chars.
Now my task is to cut the string at his 650 position. I tried strcpy,
to copy the first 650 chars into another string but I got a memory
access error. I have heard that i can use this function only to copy
500 chars is that true? What other options do i have?

No, that is NOT true. strcpy's lower limits are in the gigabyte range
when there are any limits at all. [After a few gigabytes, you
start overflowing 32 bit pointers...]

Note that if you only want to copy the leading part of a string,
strcpy is not the right function to use. I suggest you reexamine
the documentation for functions that have the letter 'n' in their
names.
 
S

Stefan Klemm

Am Mon, 15 Aug 2005 23:56:01 -0700 schrieb Patrick:
Hello

I have the following "easy" problem. I have a string which contains
1000 chars.
Now my task is to cut the string at his 650 position.
string[650] = '\0';
I tried strcpy,
to copy the first 650 chars into another string but I got a memory
access error.
man strncpy
Does the second string have enough memory?
I have heard that i can use this function only to copy
500 chars is that true?
The length of a character array is determined by NULL. The limitation is
the free heap space.
What other options do i have?
Read a good C book.


Best Regards,
Stefan
 
K

Krishanu Debnath

Patrick said:
Hello

I have the following "easy" problem. I have a string which contains
1000 chars.
Now my task is to cut the string at his 650 position. I tried strcpy,
to copy the first 650 chars into another string but I got a memory
access error. I have heard that i can use this function only to copy
500 chars is that true? What other options do i have?

Best Regards
Patrick

char *strncpy(char * restrict s1, const char * restrict s2, size_t n)
Make sure that you understand the function correctly.

Krishanu
 
S

Suman

Patrick said:
I tried it with strncpy but i get again a memory access violation....
1. Quote context.
2. Are you sure you are allocating memory before calling str*cpy()?
And, remember, you need to allocate strlen(whatever) + 1 chars, to make
room for the terminating null character. Also, post the offending code.
 
C

Coder

Patrick said:
Hello

I have the following "easy" problem. I have a string which contains
1000 chars.
Now my task is to cut the string at his 650 position. I tried strcpy,
to copy the first 650 chars into another string but I got a memory
access error. I have heard that i can use this function only to copy
500 chars is that true? What other options do i have?

Best Regards
Patrickint

I think the following code solves ur problem...

main()
{
char st1[1000],st2[650];
int i;

for (i = 0; i < 1000; i++)
st1 = 'a';

st1 = '\0';

strncpy(st2,st1,sizeof(st2));
st2[650] = '\0';

for (i = 0; st2 != '\0'; i++)
printf("%d\t%c\n",i,st2);

printf("%d",i);

}
 
G

Giannis Papadopoulos

Coder said:
I think the following code solves ur problem...

When you answer, try to be sure that what you are proposing works ok...

Shoyld always be

int main(void)
{
char st1[1000],st2[650];
int i;

for (i = 0; i < 1000; i++)
st1 = 'a';

st1 = '\0';


You have already passed the limits of your st1... You try to access
st1[1000] which is wrong and produces an error in a well-written system..
strncpy(st2,st1,sizeof(st2));
st2[650] = '\0';

Same mistake here. Moreover, strncpy() does copy the ending '\0' so
there is no need for the erroneous st2[650] = '\0';
for (i = 0; st2 != '\0'; i++)
printf("%d\t%c\n",i,st2);

printf("%d",i);


And add a
return 0;


--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
 
S

Suman

Coder said:
I think the following code solves ur problem...
No. It doesn't.

To start with, you need a few basic headers:

#include <stdio.h>
#include said:

So, what did you say, your code compiled on?
Use

int main()
{
char st1[1000],st2[650];
int i;

for (i = 0; i < 1000; i++)
st1 = 'a';

st1 = '\0';


i is now thousand, and accessing out-of-bounds isn't correct.
I warned about *this* thing only in my previous post, please
take the time out to read the whole thread. Please!
strncpy(st2,st1,sizeof(st2));
st2[650] = '\0';

for (i = 0; st2 != '\0'; i++)
printf("%d\t%c\n",i,st2);


Indentation is most welcome, btw.
printf("%d",i);

Hm. No \n at end of printf(). You sure you know what you are
doing?

Also a little
return 0;
wouldn't hurt.
 
S

SM Ryan

# Hello
#
# I have the following "easy" problem. I have a string which contains
# 1000 chars.
# Now my task is to cut the string at his 650 position. I tried strcpy,
# to copy the first 650 chars into another string but I got a memory
# access error. I have heard that i can use this function only to copy

Did you allocate enough space? Malloc for a copy of string s is like
malloc(strlen(s)+1)
The +1 being space for the zero byte terminator.
 
G

Giannis Papadopoulos

Krishanu said:
Giannis said:
Coder said:
Patrick wrote:

st2[650] = '\0';

Same mistake here. Moreover, strncpy() does copy the ending '\0' so
there is no need for the erroneous st2[650] = '\0';


No, not always. Read FAQ.

http://www.eskimo.com/~scs/C-faq/q13.2.html

Krishanu

My mistake.. I should have said that it copies the '\0' when it reaches
it at the end of the word...

However, st2[650] = '\0' is bad....

--
one's freedom stops where other's begin

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
 
C

Coder

Suman said:
Coder said:
I think the following code solves ur problem...
No. It doesn't.

To start with, you need a few basic headers:

#include <stdio.h>
#include said:

So, what did you say, your code compiled on?
Use

int main()
{
char st1[1000],st2[650];
int i;

for (i = 0; i < 1000; i++)
st1 = 'a';

st1 = '\0';


i is now thousand, and accessing out-of-bounds isn't correct.
I warned about *this* thing only in my previous post, please
take the time out to read the whole thread. Please!
strncpy(st2,st1,sizeof(st2));
st2[650] = '\0';

for (i = 0; st2 != '\0'; i++)
printf("%d\t%c\n",i,st2);


Indentation is most welcome, btw.
printf("%d",i);

Hm. No \n at end of printf(). You sure you know what you are
doing?

Also a little
return 0;
wouldn't hurt.



missing int in front of main and missing return are cut-copy-paste
errors ... i posted the code in haste sorrt for that . Anyway thanks
for pointing other errors ... does accessing 1000th element produce
out-of-bounds error ? If u declare a 1000 byte long string u can store
1000 characters from 0th element to 999th element and 1000th element
contains a NULL.Since I used a loop to store thousand 'a's I used a
NULL in 1000 th element to terminate the string.Isn't that acceptable ?
 
C

Chris Dollin

Coder said:
missing int in front of main and missing return are cut-copy-paste
errors ... i posted the code in haste sorrt for that . Anyway thanks
for pointing other errors ... does accessing 1000th element produce
out-of-bounds error ?

No, it procedures undefined behaviour.
If u declare a 1000 byte long string u can store

"you". Not "u".

You can't "declare a 1000 byte long string" in C. You can
declare a character array of 1000 elements, if you like ...
1000 characters from 0th element to 999th element and 1000th element
contains a NULL.

.... but that has elements 0..999 only.
 
S

Suman

Denis said:
No. Use int main(void).


In C, empty parentheses in the DEFINITION of a function
indicate that it takes no parameters. This definition would
provide a declaration, but not a prototype, for the defined function.
 
K

Kenneth Brody

Patrick said:
I tried it with strncpy but i get again a memory access violation....

Please include context in your replies. Google's interface is broken by
default. Others here have posted how to do it correctly. Search the
archives.

In any case, if you're still getting access violations, then you're
doing something wrong. Post a sample of code that fails.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
P

pete

Suman said:
In C, empty parentheses in the DEFINITION of a function
indicate that it takes no parameters. This definition would
provide a declaration, but not a prototype, for the defined function.

N869
Introduction
[#2] Certain features are obsolescent, which means that they
may be considered for withdrawal in future revisions of this
International Standard. They are retained because of their
widespread use, but their use in new implementations (for
implementation features) or new programs (for language
[6.11] or library features [7.26]) is discouraged.

6.11 Future language directions
6.11.4 Function declarators
[#1] The use of function declarators with empty parentheses
(not prototype-format parameter type declarators) is an
obsolescent feature.
6.11.5 Function definitions
[#1] The use of function definitions with separate parameter
identifier and declaration lists (not prototype-format
parameter type and identifier declarators) is an obsolescent
feature.
 
D

Denis Kasak

Suman said:
In C, empty parentheses in the DEFINITION of a function
indicate that it takes no parameters. This definition would
provide a declaration, but not a prototype, for the defined function.

Defining functions in this way is deprecated and is still maintained
only for compatibility with old code. There is no good reason for not
using prototypes. Furthermore, adding 'void' improves consistency, since
a function _declaration_ with empty parentheses means a completely
different thing from a function _definition_ defined in this way.

-- Denis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top