• 0

[c] reading a string on second letter


Question

Hi. I wanted to know how I could start off a string from the second letter. An example would be better:

eg.

char mybuf[100];

mybuf = "Hi.";

I want it so it is:

mybuf = "i."

So basically I am starting off at the 2nd spot. Thanks!

Also i want to add that i know how to do it when it is

char * mybuf;

but is it possible to do it with

char mybuf[100]?

Link to comment
https://www.neowin.net/forum/topic/229717-c-reading-a-string-on-second-letter/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Do it the exact same way you would with a char*: use "myBuf+1".

A char[] is the exact same thing a char*, except that with a char* u need to allocate and de-allocate memory yourself; both are pointers...

btw kjordan2001: no need to use a strncpy... a strcpy would work just fine since it would copy until the eos char (\0)...

strcpy(dest,myBuf+1);

strcpy(dest,&mybuf[1]);

  • 0
  Mouton said:
no need to use a strncpy... a strcpy would work just fine since it would copy until the eos char (\0)...

strcpy(dest,myBuf+1);

strcpy(dest,&mybuf[1]);

584723600[/snapback]

But that might be a buffer overflow. You don't know if the string fits into the buffer. If it is filled to the last byte, your strcpy would overflow the buffer by one. If you think that doesn't matter - remember how every other critical security hole involves unchecked buffers?

At the same time...

  kjordan2001 said:
strncpy(dest,&mybuf[1],strlen(mybuf)-2);

or

strncpy(dest,mybuf+1,strlen(mybuf)-2);

584723180[/snapback]

Those are subtly broken as well, with at least two bugs. They are probably just oversights: Using strlen(myBuf)-2 here doesn't make sense. Consider what happens if there are 99 non-null characters plus the '\0' at the end in myBuf. strlen() returns 99. You pass 97 as the buffer size to strncpy. It will thus truncate the string.

And it will reveal the second bug: strncpy is not guaranteed to zero-terminate the buffer. If there is not enough room for the original string in the destination buffer, the last character in the destination buffer will be the last from the source buffer that could be copied, rather than '\0'. Thus people usually do the following as a workaround:

strncpy(target, source, sizeof(target));
target[sizeof(target)-1] = '\0';

You can imagine how easy it is to forget that of course... Thus some people instead ensure that the buffer is filled with zeros to begin with....

char target[100] = { 0 };

...and then use

strncpy(target, source, sizeof(target)-1);

So strncpy never touches the very last (zero) byte. That's most likely what you had in mind.

You probably meant to use sizeof() instead of strlen() as well. Hence my gut feel that it's just an oversight.

I'm not sure if strncpy's behavior is defined if source and target overlap anyway. Though I've seen and used this so many times that it apparently works at least with popular compilers/library implementations.

  • 0
  ilmcuts said:
But that might be a buffer overflow. You don't know if the string fits into the buffer. If it is filled to the last byte, your strcpy would overflow the buffer by one. If you think that doesn't matter - remember how every other critical security hole involves unchecked buffers?

I know it won't overflow because I defined the char arrays myself with the appropriate sizes.

No way u can buffer overflow if dest and myBuf are of the same size. And I wouldn't see why you would not make them the same size. Especially since he's using char arrays, and not pointers + mem allocation...

I think choosing the size of your arrays correctly is much easier to deal with than using strncpy then having to think/check for the \0...

  • 0
  Quote
So strncpy never touches the very last (zero) byte. That's most likely what you had in mind.

You probably meant to use sizeof() instead of strlen() as well. Hence my gut feel that it's just an oversight.

Yeah, I meant sizeof. However, I was right on the -2 part, since he's excluding the first letter.
  Quote
btw kjordan2001: no need to use a strncpy... a strcpy would work just fine since it would copy until the eos char (\0)...

Yeah, that would work too, guess I always go the long hard way :wacko:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • $100 billion of the $200 billion is in N.Y. State over the upcoming 20 years plus. Obviously 20+ year spending plans are subject to change.
    • I own one somewhere and even got the email from Amazon letting me know about the recall but not sure I'll be able to find it. Haven't seen it in months.
    • Steam games now detail what accessibility features they offer on store pages by Pulasthi Ariyasinghe Valve has pushed a new update for its Steam platform, and it's fully focused on accessibility. The latest change makes it both easier for developers to show what accessibility options they offer within their games and for players to find options they require to enjoy titles. "This update comes after gathering valuable feedback from developers as well as players with disabilities," says Valve in the announcement blog post. "Over 5,000 applications have added details about their accessibility support, with more developers updating their games each day." Here are the options currently offered by Valve: Gameplay: Adjustable Difficulty: Players can adjust gameplay difficulty. Save Anytime: Players can save gameplay using both automatic and manual saves. Saving can be performed at any point in the game. Audio: Custom Volume Controls: Players can adjust the volume of the audio. Different types of audio can be muted independently from each other. Narrated Game Menus: Players can listen to game menus with narrated audio. Stereo Sound: Players can identify how far to the left or right sounds are coming from. Surround Sound: Players can identify how far in any direction sounds are coming from. Visual: Adjustable Text Size: Players can adjust text size. (In-game text, menu text, character dialog text, subtitle text) Subtitle Options: Players have options to customize the display of subtitles for all spoken content and essential audio information. Color Alternatives: Gameplay doesn't rely on colors to communicate important information, or players have an option to adjust colors used for distinguishing information. Camera Comfort: Players have an option to adjust or disable uncomfortable camera movement such as screen shaking, camera bob, or motion blur, or the game doesn't feature these effects. Input: Keyboard Only Option: Players can play the game with just a keyboard and no other additional input mechanisms such as a mouse or controller. Mouse Only Option: Players can play the game with just a mouse and no other additional input mechanisms such as a keyboard or controller. Touch Only Option: Players can play the game with just touch controls and no other additional input mechanisms such as a mouse, keyboard or controller. Playable without Timed Input: Players have an option to adjust gameplay to not require precisely timed button presses ("quick time events") or gameplay does not require such button presses. Text-to-speech: Text chat can be narrated out loud in real time. Speech-to-text communications: Voice chat can be read as a text transcript in real time. These can be found when searching in the filters as well as inside games' store pages in the features section on the right side, as seen in the screenshot above. While developers are not required to mention accessibility options of their games on store pages, Valve says it is highly recommended to make sure players that require it can find relevant games more easily. "We've worked to make it as easy as possible for developers to indicate these features are available by using feedback to standardize these options as much as possible," added the company.
    • They have a concept of a plan. How fitting.
    • As Total War turns 25, Creative Assembly is teasing a "new era" for the strategy franchise by Pulasthi Ariyasinghe Creative Assembly has now officially been making Total War games for 25 years. It was in June 2000 that the UK-based studio put forth Shogun: Total War for PC, with Electronic Arts publishing the ambitious project. With a quarter of a century now behind the series, the developer is now looking towards the "next era" of Total War. The studio will host its first-ever Total War showcase in December 2025, which is described as a "flagship video presentation unveiling what’s next for the franchise." Creative Assembly said that the showcase will have announcements for new games as well as content for existing titles, with developer commentary also happening regarding the future of the series. "We’re honoured and humbled to be one of the few video games that get the chance to celebrate such a huge milestone, and we want to thank colleagues past and present, as well as our fans for all the support," says Total War vice president Roger Collum. "2025 marks an exciting new era for the franchise as we reveal what’s next for Total War, and we can’t wait to celebrate throughout the year with everyone." Read an anniversary letter from Roger Collum regarding the studio's history, Total War beginnings, and more over here. Prior to the showcase, Creative Assembly will be hosting retrospective video interviews, developer livestreams, and more behind-the-scenes content regarding the series starting this August. There will also be multiplayer tournaments for fans, giveaways, Q&A sessions with developers, mod spotlights, and more events happening during this period. As the studio celebrates the occasion, a massive Total War historical games sale has just gone live on Steam. This has discounts of up to 75% across everything from the original Shogun: Total War to the latest entry, Total War: PHARAOH. The discounts will last until June 26.
  • Recent Achievements

    • One Month Later
      POR2GAL4EVER earned a badge
      One Month Later
    • One Year In
      Orpheus13 earned a badge
      One Year In
    • One Month Later
      Orpheus13 earned a badge
      One Month Later
    • Week One Done
      Orpheus13 earned a badge
      Week One Done
    • Week One Done
      serfegyed earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      544
    2. 2
      ATLien_0
      250
    3. 3
      +Edouard
      163
    4. 4
      +FloatingFatMan
      157
    5. 5
      Michael Scrip
      110
  • Tell a friend

    Love Neowin? Tell a friend!