在char数组指针内分配值 [英] assigning a value within a char array pointer

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

问题描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX40 40

/* run this program using the console pauser or add your own getch, system("pause") or input loop */


Int40 *parseString(char *str)
{
    Int40 *p;
    char *ptr;
    int i, len, value, ptrValue;

    printf("%s\n", str);


        for(i = 0; i < 40; i++)
{
    if(str[i] == 'a')
    {
        printf("%c\n", str[i]);
            str[i] = '0';
        printf("%c\n", str[i]);
    }

    }
}

int main(int argc, char *argv[])
 {
 // char string[40] = "
 // char *str = string;
    Int40 *p;


    parseString("0123456789abcdef0123456789abcdef01234567");

    return 0;
}

在我的parseString函数中,两个printf之间语句,我正在尝试将'a'的特定点处的值分配为0。我不确定应该如何尝试此操作,并且我也希望变量保持相同。 / p>

In my parseString function, between the two printf statements, I'm trying to assign the value at the specific point of 'a' to 0. I'm not sure how I'm supposed to be attempting this, and I would like to keep the variables the same as well.

推荐答案

您的问题是,您要传递给 parseString 指针
字符串文字。修改字符串文字的内容是未定义的行为
,并且大多数情况下字符串文字位于只读内存中,这就是
程序崩溃的原因。

Your problem here is that you are passing to parseString a pointer to a string literal. Modifying the contents of string literals is undefined behaviour and most of the times string literals reside in read only memory, that's why your program crashes.

您必须创建一个数组并使用所需的字符串对其进行初始化,然后
将该数组传递给functi

You have to create an array and initialize it with the string you want, then pass that array to the function:

#include <string.h>

void parseString(char *str, size_t len)
{
    printf("%s\n", str);


    for(size_t i = 0; i < len; i++)
    {
        if(str[i] == 'a')
        {
            printf("%c\n", str[i]);
                str[i] = '0';
            printf("%c\n", str[i]);
        }
    }
}

int main(int argc, char *argv[])
{
    char text[] = "0123456789abcdef0123456789abcdef01234567";

    parseString(text, sizeof text / sizeof *text);

    return 0;
}

将数组传递给函数时要记住,该函数仅
指向数组第一个元素的指针。因此,被调用的函数
无法确定数组的长度。最好也传递
数组的长度。这就是为什么我在 parseString 中将 size_t len 添加为参数
的原因。在声明 array main 中,我用<$ c $计算数组的
长度c> sizeof文本/ size * text 。请注意,如果您在 parseString 中执行了 sizeof str / sizeof * str ,这仅适用于纯数组
,您
肯定会得到错误的结果,这就是为什么您应该始终传递数组的
长度的原因。

Bear in mind when you pass an array to a function, the function gets only a pointer to the first element of the array. For that reason the function being called cannot determine the length of the array. It's better practice to pass the length of the array as well. That's why I added size_t len as an argument in parseString. In main where the array is declared, I calculate the length of the array with sizeof text / size *text. Note that this only works with pure array, if you did sizeof str / sizeof *str in parseString, you will definitively get a wrong result, that's why you should always pass the length of the array.

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

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