• 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

    • HOLY MOTHER, did not see that one coming. if they can get Rick Moranis to participate, this will be me: https://tenor.com/5P2L.gif
    • Micron announced a $200 billion investment plan in the US by Hamid Ganji Memory chipmaker Micron Technology has announced a $200 billion investment plan in the US to bolster its chip manufacturing operations. The investment focuses on Idaho, New York, and Virginia as production and R&D hubs. According to Micron's statement, the company's new investment plan in the US includes $150 billion in domestic memory manufacturing and $50 billion in R&D operations, which can finally create around 90,000 direct and indirect jobs for Americans. "These investments are designed to allow Micron to meet expected market demand, maintain share and support Micron's goal of producing 40% of its DRAM in the U.S.," Micron said. In addition to prior investments, Micron also said it plans to spend $30 billion on building a second leading-edge memory fab in Boise, Idaho, and modernizing its existing manufacturing facility in Manassas, Virginia. The memory chipmaker firm also has an ongoing plan to build a mega fab in New York. Micron's investment brings advanced packaging capabilities to the US, enabling long-term growth in High-Bandwidth Memory (HBM), a key component in AI data centers. "This approximately $200 billion investment will reinforce America's technological leadership, create tens of thousands of American jobs across the semiconductor ecosystem, and secure a domestic supply of semiconductors—critical to economic and national security," Micron CEO Sanjay Mehrotra said. The timeframe of Micron's investment in the US has yet to be determined. Still, it could be accomplished over the next four years as the company hailed Trump's administration for facilitating the investment. Meanwhile, Micron is not the only chip maker to unveil plans to invest hundreds of billions of dollars in the US under the Trump administration. In April, NVIDIA also announced a whopping $500 billion investment in the US to produce AI servers and other related gear. Other tech giants like TSMC, Apple, OpenAI, and SoftBank have also announced massive investment plans under the Trump administration.
    • Until they add a better theme I'm not using it.
    • New Outlook for Windows is getting another offline feature by Sayan Sen Microsoft recently announced that it would block more attachment file types for New Outlook for Windows and Outlook for the Web. The company said that it is doing so in order to improve the security of its mail app. Another change Microsoft announced this week regarding attachments is better offline support for such files on its New Outlook for Windows app. The company announced it on the Microsoft 365 (M365) roadmap website under a new message ID 496371. In the description of the message headlined "Outlook: add attachments while offline in the new Outlook for Windows", the tech giant writes: "We are adding more functionality to offline mode in the new Outlook for Windows. You will now be able to add attachments while drafting your email without an internet connection." Not much else has been stated, though the entry does reveal that Microsoft expects to start rolling it out next month, July 2025. You can find the M365 roadmap message here. This is the second time offline support for attachment files is receiving an upgrade. In December 2024, Microsoft added the ability to "open attachment files" without an internet connection. The tech giant has been trying to improve offline operation on its New Outlook app for a while. Last month, Microsoft also added offline Calendar support so that internet connectivity is not necessary. The idea is to try to get more users to switch from the Classic app to the New one. Speaking of classic Outlook, Microsoft recently confirmed that a major Calendar feature upgrade has led to several bugs on the classic app, as it breaks in many ways. If you are someone experiencing problems with it, check out this dedicated article, as Microsoft has provided workarounds for several issues.
  • Recent Achievements

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

    1. 1
      +primortal
      522
    2. 2
      ATLien_0
      263
    3. 3
      +FloatingFatMan
      203
    4. 4
      +Edouard
      171
    5. 5
      snowy owl
      122
  • Tell a friend

    Love Neowin? Tell a friend!