• 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

    • Oh, what if the AI skips over some "editor's" personally cherished lie in an article? The Horror!
    • eagerly awaiting a version that runs on android tablets
    • I grew up with the Beach Boys. Even saw them in concert in the 70's. Brian suffered from mental issues all his life. May you find peace wherever you are.
    • Wikipedia suffers backlash from human editors over AI summaries, prompting feature pause by David Uzondu Wikipedia editors have pushed back against plans from the Wikimedia Foundation to test AI-generated article summaries, powered by Aya, the open-weight AI model from Cohere. The non-profit has now paused the project. The decision came after a swift and overwhelmingly negative reaction from its community. As first reported by 404Media, the plan involved a two-week, opt-in trial on the mobile version of Wikipedia. But the volunteer editors who build the encyclopedia met the idea with immediate and fierce opposition. The project's discussion page became a torrent of rejection. It included simple comments like "Yuck" and blunt declarations like "strongest possible oppose" and "Absolutely not." One editor argued that a test would cause "immediate and irreversible harm to our readers and to our reputation as a decently trustworthy and serious source." They noted that Wikipedia has built its name on being sober and reliable, not flashy. Another feared it would destroy the site's collaborative model. They argued that while the "collective mass" of human editors "evens out into a beautiful corpus," the AI would install "one singular editor with known reliability and NPOV [neutral point-of-view] issues" at the very top of an article. That same editor also noted the following: For context, this is what AI-generated summaries on the platform was supposed to look like: Image: 404Media It is not hard to see why they are so protective. The editors' fears are grounded in recent and very public failures of AI features from tech giants. For example, Google's AI overviews recently hit 1.5 billion monthly users. The feature became a laughingstock for telling people to put glue on their pizza and that a dog had played in the NBA. This is the kind of humiliating error Wikipedia's community is desperate to avoid, as it would undermine two-plus decades of careful work. We also saw the potential for reputational damage back in January. That was when Apple's AI feature falsely generated a notification claiming that Luigi Mangione had died by suicide. The man was actually alive and in custody. On the site's technical discussion page earlier today, Marshall Miller (MMiller), a Senior Director at the Wikimedia Foundation, posted an update acknowledging the feedback. He admitted, "It's clear we could have done a better job introducing this idea," and confirmed the experiment was paused. The Foundation says the goal was to explore accessibility for different readers. While this specific test is off the table, the organization still wants to use new technologies. Miller ended with a promise: "We do not have any plans for bringing a summary feature to the wikis without editor involvement." A WMF spokesperson also told 404Media that though the feature has been paused, the foundation is still interested in AI-generated summaries. The spokesperson insisted the goal was to eventually build moderation systems where "humans remain central" and called this kind of backlash feedback part of what makes Wikipedia a "truly collaborative platform."
    • I see, yeah that makes sense. I have been in situations where I barely did not crush badly on the road due to other driver starting to change lanes into another car - freaked out last second and avoided it by crashing into the side of the bridge instead. i got away because I quickly changed lanes 2 times in a couple of second and unlike that idiot I did not lose control big part of this was my car was good 😊 (audi a7) vs the old van the crashed driver was driving would AI be able to react and quickly change lanes twice both time barely avoiding collision … I don’t know my car systems pumped the breaks and tried to warn me with a beep and vibration but if I slammed the breaks the car behind me would hit me then again I have BMW driver training and a good car - so I have no idea how robot taxi would react i am not sure extreme fast lane changes would be programmed in - it is dangerous as hell unless you are FULLY aware, and have done it before but it is a general risk to do it especially in the conditions with bad weather and when you are not driving a sports car with 4 wheel drive and very good control
  • Recent Achievements

    • Collaborator
      CHUNWEI earned a badge
      Collaborator
    • Apprentice
      Cole Multipass went up a rank
      Apprentice
    • Posting Machine
      David Uzondu earned a badge
      Posting Machine
    • One Month Later
      Stokenking earned a badge
      One Month Later
    • One Month Later
      Kevin Jones earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      537
    2. 2
      ATLien_0
      266
    3. 3
      +Edouard
      193
    4. 4
      +FloatingFatMan
      181
    5. 5
      snowy owl
      135
  • Tell a friend

    Love Neowin? Tell a friend!