指针数组子 [英] pointer substrings arrays

查看:131
本文介绍了指针数组子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着字符串复制部分使用指针另一个字符串。我得到的字符串开始在正确的地方复制,虽然它不超过计数后停止。另外,字符串不从源字符串,而不是从结果参数复制

 的#include<&stdio.h中GT;字符* GETSUB(为const char *原稿,诠释开始,诠释计数,字符* RES);INT主要(无效)
{
    为const char原稿[] =一二三
    CHAR RES [] =123456789012345678;    的printf(%S \\ n,GETSUB(原稿,4,3,RES));    返回0;
}字符* GETSUB(为const char *原稿,诠释开始,诠释计数,字符* RES)
{
    为const char *镜检查=原稿;    而(*原稿)
    {
        如果(开始> =(原稿 - SCOPY))及和放大器; (RES-SCOPY<计数))
        {
            * RES ++ = *原稿++;
        }
        其他
            *原稿++;
    }    返回水库;
}


解决方案

最大的失误是,你计算两个不相关指针的差异,水库 - 镜检查(我想 sourceCopy 镜检查在现实code,或者反过来)。计算指针的区别,如果两个指针指向到(或一个过去的结束)相同的数组才有意义。由于写的,任何事情是否会被都复制取决于两个阵列的任意位置。

 如果(开始> =(原稿 -  sourceCopy))及和放大器; (RES-SCOPY c为C))
        {
            * RES ++ = *原稿++;
        }
        其他
            *原稿++;

反正不指望有多少字符,如果任何在所有复制拷贝。

另一个错误是,你不0终止副本。

有一个正确的实施将

 的char * GETSUB(为const char *原稿,诠释开始,诠释计数,字符* RES)
{
    从=原稿,*以RES =的char *;
    //检查启动位置是否原稿中
    对于(;开始> 0; --start,从++)
    {
        如果(*从== 0)
        {
             水库[0] = 0;
             返回水库;
        }
    }
    //复制count个从对人物
    对于(;计数大于0和放大器;&放大器; * FROM; --count)
    {
        *为++ = *从++;
    }
    // 0终止
    *为= 0;
    //返回副本开始,改为返回是否应该返回结束
    返回水库;
}

Im trying to copy part of a string to another string using pointers. My resulting string starts to copy at the correct place though it doesn't stop after exceeding the count. Also the string isn't copy from the source string rather than from the result parameter

#include <stdio.h>

char *getSub(const char *orig, int start, int count, char *res);

int main(void)
{
    const char orig[] = "one two three";
    char res[] = "123456789012345678";

    printf("%s\n",getSub(orig, 4, 3, res));

    return 0;
}

char *getSub(const char *orig, int start, int count, char *res)
{    
    const char *sCopy = orig;

    while (*orig)
    {
        if (start >= (orig - sCopy)) && (res-sCopy < count))
        {
            *res++ = *orig++;
        }
        else
            *orig++;
    }

    return res;
}

解决方案

The big mistake is that you're calculating the difference of two unrelated pointers, res - sCopy (I suppose sourceCopy is also sCopy in the real code, or the other way round). Calculating the difference of pointers is only meaningful if both pointers point into (or one past the end of) the same array. As written, whether anything gets copied at all depends on the arbitrary locations of the two arrays.

        if (start >= (orig - sourceCopy)) && (res-sCopy < c))
        {
            *res++ = *orig++;
        }
        else
            *orig++;

anyway, that doesn't count how many characters are copied if any are copied at all.

Another mistake is that you don't 0-terminate the copy.

A correct implementation would be

char *getSub(const char *orig, int start, int count, char *res)
{
    char *from = orig, *to = res;
    // check whether the starting position is within orig
    for( ; start > 0; --start, ++from)
    {
        if (*from == 0)
        {
             res[0] = 0;
             return res;
        }
    }
    // copy up to count characters from from to to
    for( ; count > 0 && *from; --count)
    {
        *to++ = *from++;
    }
    // 0-terminate
    *to = 0;
    // return start of copy, change to return to if end should be returned
    return res;
}

这篇关于指针数组子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆