• 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

    • Microsoft shares Windows 10 ESU key guide for office PCs that don't support Windows 11 by Sayan Sen Recently, the Indian government issued a caution about the upcoming end of support for Windows 10 which is about four months away. Interestingly, on that very day, Microsoft made a major announcement regarding this as the tech giant debuted free enrollment options wherein support would be extended for another year at no extra cost. Microsoft is also trying to make it easy to set up Extended Security Updates (ESU) with a new tool. You can see in the image below: New ESU enroll wizard Following that, today, the company has shared a new guide on how to activate ESU via Intune in the case of Windows 10 office PCs that are not supported on Windows 11. First up, Microsoft has reminded users of the prerequisites of Windows 10 ESU: The system needs to be on Windows 10 22H2 with KB5046613 (November 2024 Patch Tuesday) or newer. The device needs access to the network endpoints required for client activation. The process that is used for activation needs administrative privileges on the device. The guide contains several PowerShell scripts and are meant for IT admins and system admins. One of those is a validation script "Check-Win10ESUPrereq.ps1" that Microsoft has published to help "verify a device has the required version and patch level for Windows and it can communicate to the required network endpoints." Essentially, it checks if the aforementioned requirements are satisfied and can be used for troubleshooting against those prerequisites. After the "validation," Microsoft says there are two ways to proceed with the ESU key activation. First. it can be done by either Remediation through: Detection Script (Win10ESUActivation-detect.ps1) and Remediation Script (Win10ESUActivation-remediate.ps1). And second, by Win32 Application with Win32 Install Script (Win10ESU-install.ps1) and Win32 Detection Script (Win10ESU-detection.ps1). In the case of Remediations, Microsoft mentions "some licensing considerations" due to which "some smaller companies may not have that option." Finally, Microsoft has also shared some Activation IDs for manual verification via Slmgr VB script for obtaining volume activation information: Win10 ESU Year1: f520e45e-7413-4a34-a497-d2765967d094 Win10 ESU Year2: 1043add5-23b1-4afb-9a0f-64343c8f3f8d Win10 ESU Year3: 83d49986-add3-41d7-ba33-87c7bfb5c0fb You can check the full guide and the details here on Microsoft's official Tech Community website.
    • It blows me away how overpriced Synology is. $600+ for a 3+ year old SATA NAS with only gigabit networking!! Even for 2022 that is pretty low-end hardware. Yes, the Synology software is fantastic, but if we say this hardware is worth $300 max (and that is even giving a generous premium to the fact that it is made by a respected OEM), their software is NOT worth an additional $300, especially considering it is locked to the hardware it shipped on and you have to pay that premium again for your next devices. Important correction to the specs table above. Saying Disk Capacity is 72TB and even going further to detail that is 4x18, strongly implies it comes with those disks, which it does not. I would rather see it say "Max Capacity: 72TB (disks not included)."
    • well again if w11 adoption its so high there's no reason why they try to explain us why is "better" to move to w11. If they are so right about their downgraded UI, why they add some of their UI elements back things like never combine and now small icons. Last time i check majority is still on 10 over 11 so yeah millions of users that did not upgrade to w11
    • Size 15. In the first week I managed to get a full week before having to recharge from 10%. I looked online and Samsung recommends recharging it when it gets to 20-30% to preserve the battery life. In the second week, started wearing my Galaxy Watch6 Classic again, because I read that it offsets sensors to the Watch, meaning the battery lasts even longer... and a week on (today) the ring was at 31% when I recharged it fully again, so a difference of over 20% in combination with the Watch. By the way I do not wear my Watch to bed, so it is only the Ring doing the sleep tracking (which is hit or miss tbh) it stops tracking for an hour or two in the night, which is really annoying. I had the same thing with the Watch, and I found it uncomfortable to have on in bed. I have been reading that the battery can start to go bad even after the first week so I am glad it isn't affecting me (yet).
    • Visual Studio gets even smarter with more AI models and billing updates by Usama Jawad Visual Studio and Visual Studio Code are among the most popular integrated development environments (IDEs) out there. The tools boast more than 50 million monthly active users, which isn't surprising considering their platform agnosticism, deep integration with the Microsoft ecosystem, and the power of GitHub Copilot. Now, Microsoft is looking to entice even more customers who are eager to use AI models to boost their productivity during the development process. In a blog post, Microsoft has stated that it has updated the AI models list available in Visual Studio to default to smarter options. For example, Copilot will now use GPT-4.1 rather than GPT-4o, since it offers better responses with faster performance. In addition, users can now select between the following models to enhance their coding experience based on their preferences: Claude Sonnet 4 Claude Opus 4 Claude Sonnet 3.5 Claude 3.7 (non-thinking and thinking) OpenAI o3 mini Gemini 2.0 Flash Gemini 2.5 Pro Microsoft has noted that your selected model will persist across your workflows, so if you're unsure about which model to leverage, you can refer to its documentation here. Furthermore, Visual Studio is making it easier to switch between models that are included in your plan through a prompt in the model selector. When it comes to billing updates, Microsoft has built a new Copilot Consumptions user experience that can be accessed by navigating to the Copilot badge present in the top-right corner of the IDE. As the name suggests, this panel shows your consumption in an easily digestible format. You can also click on Manage Plan, which will take you to the GitHub website. It is important to note that some models are request-heavy, and Visual Studio will now indicate this to you while you are selecting your model. If you exhaust your premium requests, you will shift to a standard model seamlessly. You should also keep in mind that the GitHub Copilot pricing plans have been updated, and the billing experience in Visual Studio does reflect them.
  • Recent Achievements

    • Conversation Starter
      Kavin25 earned a badge
      Conversation Starter
    • One Month Later
      Leonard grant earned a badge
      One Month Later
    • Week One Done
      pcdoctorsnet earned a badge
      Week One Done
    • Rising Star
      Phillip0web went up a rank
      Rising Star
    • One Month Later
      Epaminombas earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      529
    2. 2
      ATLien_0
      207
    3. 3
      +FloatingFatMan
      168
    4. 4
      Michael Scrip
      149
    5. 5
      Steven P.
      122
  • Tell a friend

    Love Neowin? Tell a friend!