将带有数字和字母的字符串放入结构中的int指针中 [英] put a string with numbers and letters into an int pointer within a struct

查看:165
本文介绍了将带有数字和字母的字符串放入结构中的int指针中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typedef struct Int40
{
  int *digits;
} Int40;




  Int40 *parseString(char *str)
{
    Int40 *p;

    int i;
    int *intPtr;
    printf("%s\n", str);

    p->digits = malloc(sizeof(str) + 1);

    for(i = 0; i < strlen(str); i++)
    {
        p->digits = atoi(str);
        printf("%d\n", p->digits);
    }

int main(int argc, char *argv[])
 {

    Int40 *p;

    parseString("0123456789abcdef0123456789abcdef01234567");
    return 0;
}

我试图将字符串012345679abcdef0123456789abcdef01234567放入结构指针数字,但是我不确定我该怎么做。

I am attempting to put the string "012345679abcdef0123456789abcdef01234567" into the struct pointer digits, however I am not sure how I should be doing this.

我当前程序的错误是'传递atoi的参数1使整数指针没有强制转换

My error with this current program is 'passing argument 1 of atoi makes pointer from integer without a cast

如果我从str [i]中移除[i]并且p-> digits [i]

And if I remove the [i] from str[i] and p->digits[i]

p->digits[i] = atoi(str[i]);

然后我的结果仅返回123456789

then I am returned only 123456789 for my result

编辑**
我在parseString函数中添加了一个malloc
我试图弄清楚如何使用struct

EDIT** I added a malloc into the parseString function I'm trying to figure out how to convert the char *str into int format using the int *digits in the struct

推荐答案

看起来你对一些事情感到困惑。

It looks like you are confused about a few things.

1)内存分配

 p->digits = malloc(sizeof(str) + 1);

这是字符串的分配,但是 p->数字 int 类型的指针。

This is allocation for string but p->digits is a pointer of int type.

2)函数 atoi

int atoi(const char *str);

返回 int 值。
类型 int 的变量的最大值是 2147483647
检查: limits.h

Returns int value. Maximum value for a variable of type int is 2147483647 Check: limits.h.

如果您使用 long long int 它可以为您提供 19 位数。
你需要比更长的数字吗?long long int 可以给你吗?

If you use long long int it can give you 19 digits. Do you need more digits than long long int can give you?

3)记得分配结构的内存,并记得释放它。

3) Remember to allocate memory for the structure and also remember to release it.

检查以下程序:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct LLint19
{
   long long int *digits;
} LLInt19;

long long int sg7_atoi(const char *c)
{
    long long int value = 0;
    int sign = 1;
    if( *c == '+' || *c == '-' )
    {
        if( *c == '-' ) sign = -1;
        c++;
    }
    while (*c >= '0' && *c <= '9') // to detect digit == isdigit(*c))
    {
        value *= 10;
        value += (int) (*c-'0');
        c++;
    }
    return (value * sign);
}

LLInt19 *parseString(char *str)
{
    LLInt19 *p;
    long long int *value;

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

    value = malloc (sizeof(long long int) ); // allocate memory for long long int value

    p = malloc( sizeof(LLInt19) );           // allocate memory for the structure

    *value  = sg7_atoi(str);                 // do a conversion string to long long int

    p->digits = value;

    return p;
}

int main(int argc, char *argv[])
{
    LLInt19 *p;

    char test1[] = "1234567890123456789";

    p = parseString(test1);

    printf("Output str: %lld \n", *p->digits);

    free(p->digits);
    free(p);

    return 0;
}

输出:

Input  str: 1234567890123456789                                                                                                             
Output str: 1234567890123456789 

这篇关于将带有数字和字母的字符串放入结构中的int指针中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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