Page 1 of 1

Reputation:

# Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:21 AM

If I were to do
(length of string is 10 chars)
```strncpy(string, temp, (len-1));
```

is it the same as
```string[10] = '\0';
```
?
Is This A Good Question/Topic? 0

## Replies To: Removing last char of a string by moving null char left 1

### #2 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:30 AM

what character does temp hold? and is string already a NULL-terminated string? Also if string is 10 characters long, it would be
```string[9] = '\0'
```
because you start counting from zero not one.

### #3 jimblumberg

Reputation: 5488
• Posts: 17,064
• Joined: 25-December 09

## Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:30 AM

Since you have a C-string with a length of 10 characters string[10] = '\0' would be writing outside of the bounds of the array. Remember in C arrays go from 0 to size -1. So in this case it should be: string[9] = '\0'.

Jim

This post has been edited by jimblumberg: 23 February 2011 - 10:31 AM

Reputation:

## Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:38 AM

vividexstance, on 23 February 2011 - 10:30 AM, said:

what character does temp hold? and is string already a NULL-terminated string? Also if string is 10 characters long, it would be
```string[9] = '\0'
```
because you start counting from zero not one.

jimblumberg, on 23 February 2011 - 10:30 AM, said:

Since you have a C-string with a length of 10 characters string[10] = '\0' would be writing outside of the bounds of the array. Remember in C arrays go from 0 to size -1. So in this case it should be: string[9] = '\0'.

Jim

Ah, that's right!

Also, 'temp' was a copy of string, and 'string' is a null terminated string.
I was simply testing ways, because I can't figure out how to code this..

I'm supposed to remove the last element of a string by moving the null character left one place. I thought that if I were to simply copy the string - 1, it would solve it, but it doesn't .

Reputation:

## Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:40 AM

Kom, on 23 February 2011 - 10:38 AM, said:

vividexstance, on 23 February 2011 - 10:30 AM, said:

what character does temp hold? and is string already a NULL-terminated string? Also if string is 10 characters long, it would be
```string[9] = '\0'
```
because you start counting from zero not one.

jimblumberg, on 23 February 2011 - 10:30 AM, said:

Since you have a C-string with a length of 10 characters string[10] = '\0' would be writing outside of the bounds of the array. Remember in C arrays go from 0 to size -1. So in this case it should be: string[9] = '\0'.

Jim

Ah, that's right!

Also, 'temp' was a copy of string, and 'string' is a null terminated string.
I was simply testing ways, because I can't figure out how to code this..

I'm supposed to remove the last element of a string by moving the null character left one place, using 'pointers', so I can't simply do
```string[9] = '\0'
```
. I thought that if I were to simply copy the string - 1, it would solve it, but it doesn't .

Reputation:

## Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:41 AM

Ahh!! Sorry for the spam, I meant to edit my post, but I guess guests can not edit their posts .
The revised post is in the second one

### #7 jimblumberg

Reputation: 5488
• Posts: 17,064
• Joined: 25-December 09

## Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:47 AM

But you can use strncpy() just the first 9 characters to your new location. Just be sure to attach the end of string char to the end. See this link: strncpy() it has the description along with an example.

Jim