简体   繁体   English

从关联指针数组中按字符读取字符串char

[英]Read a string char by char from an associative pointer array

Solved 解决了

I create a new char *ch malloc(strlen) and loop that string char by char. 我创建一个新的char *ch malloc(strlen)并逐个循环该字符串char。 Then i copy it back using memcpy(ch, &stringpool[index], len) Of course, afterwards free(ch) . 然后我使用memcpy(ch, &stringpool[index], len)将其复制回去memcpy(ch, &stringpool[index], len)当然,之后是free(ch)


I hope the title of my question is correct. 我希望我的问题的标题是正确的。

I have a stringpool 我有一个字符串池

char **string_pool;

that gets initiated in a function like 在类似的函数中启动

string_pool = malloc( sizeof(char *) * 1024);

i get strings from stdin and scanf them into the array 我从标准输入中获取字符串并将其扫描到数组中

scanf("%[^\n]s", &string_pool[index]);

so i can print it out using printf 所以我可以用printf打印出来

printf("gets %s\n", &string_pool[index]);

how can i 我怎样才能

  • get the length of string_pool[index] 获取string_pool [index]的长度
  • read string_pool[index] char by char in a loop 循环读取char读取string_pool [index] char

Thank you 谢谢

Edit 编辑

Maybe i should explain it a bit more, its a virtual machine with a virtual instruction set and a program like 也许我应该多解释一下,它是一个带有虚拟指令集和类似程序的虚拟机

push 1
read
gets

should : 应该 :

  • push 1 on the stack -> let x be 1 将1压入堆栈->让x为1
  • read stdin as string into string_pool[x] 将stdin作为字符串读入string_pool [x]
  • push all characters onto the stack 将所有字符压入堆栈

the functions looks like 功能看起来像

    case GETS: {
        int index = popv(); // index is already on top of the stack
        int strl = strlen(&string_pool[index]);

        printf("gets %s with a length of %d\n", &string_pool[index], strl);
        // pseudo code
        // push each char as integer on the stack
        foreach(char in string_pool[index]) push((int)char);

        break;
    }

    case READ: {  
        int index = popv();          
        scanf("%[^\n]s", &string_pool[index]);
        break;
    }

    case WRITE: {  
        int index = popv();          
        printf("%s", &string_pool[index]);
        break;
    }

My problem is in the GETS case. 我的问题是在GETS案中。 I want to push every char as int onto the stack. 我想将每个char作为int推入堆栈。

 char **string_pool; string_pool = malloc( sizeof(char *) * 1024); 

Allocates 1024 pointers-to-char . 分配1024个指向char的指针 ( string_pool is a pointer-to-pointer-to-char ) Each of the pointers are uninitialized and point to no valid storage that can be used. string_pool是一个指向char的指针)各个指针未初始化,并且指向没有可用的有效存储。 Before you can use each pointer, you must make them point to valid memory by allocating memory and assigning the starting address to each pointer (so that each pointer "points" to valid memory). 在使用每个指针之前,必须通过分配内存并将起始地址分配给每个指针,使它们指向有效内存(以便每个指针“指向”有效内存)。

To allow proper sizing and allocation for each string your read, you will either use fgets with a fixed buffer sufficient to store the longest input you expect, or you can use POSIX getline which will allocate storage for input as required. 为了对读取的每个字符串进行适当的大小调整和分配,您可以使用带有固定缓冲区的fgets来存储所需的最长输入,或者可以使用POSIX getline来根据需要为输入分配存储空间。 Using fgets you would do something similar to: 使用fgets可以执行以下操作:

#define MAXCHR 2048
...

char buffer[MAXCHR];

fputs ("enter string: ", stdout);
if (fgets (buffer, MAXCHR, stdin) == NULL) {
    fputs ("(user canceled input.)\n", stderr);
    return 1;
}

size_t len = strlen (buffer);   /* get the length of input (with '\n') */
if (len && buffer[len - 1] == '\n')    /* trim the '\n' from end */
    buffer[--len] = 0;          /* by overwriting with nul-character */


string_pool[index] = malloc (len + 1);      /* allocate storage for buffer */
if (string_pool[index] == NULL) {           /* validate allocation */
    perror ("maklloc-string_pool[index]");  /* handle error */
    return 1;  /* or break read loop; */
}
/* copy buffer to string_pool[index], advance index */
memcpy (string_pool[index++], buffer, len + 1); 

You would generally do this within your read loop only allowing input while index < 1024 to insure you did not attempt to allocate storage for one pointer too many. 通常,您将在读取循环内执行此操作,仅允许在index < 1024输入,以确保您不会尝试为一个指针分配过多的存储。

Look things over and let me know if you have questions. 仔细检查一下,如果您有任何问题,请告诉我。

You must allocate space for each string. 您必须为每个字符串分配空间。 So, for example, 因此,例如

string_pool[0] = malloc( 100 );

will then allow you to read a string that is up to 99 characters long into string_pool[0] (the extra space is needed for the terminating null). 然后将允许您将一个最长为99个字符的字符串读取到string_pool [0]中(终止null需要额外的空间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM