将char连接到char *

我正在尝试连接/附加一个char *和char。我不是c专家,如果这个问题没有道理,请原谅我。我尝试了多种方法,但似乎无济于事。

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

static void assignmode()
{
      char modeString[] = "SPI_LOOP,SPI_CPOL";
      char *temp[]= (char *)malloc(strlen(modeString));
      int i,j;
      printf("Length of string a = %ld \n",strlen(modeString));
      for(i=0; i<= strlen(modeString); i++)
      {
        j =0;
        if(!(modeString[i] == ','))
        { 
          printf("Char: %c \n",modeString[i]);
          temp[j] = modeString[i];
          temp[j+1] = '\0';
          j++;
        }else
        {
          j = 0;
          printf("temp: %s\n",temp);
        }
      }
}

int main()
{
    assignmode();
}

modetest.c:36:19:警告:赋值使指针从整数开始而没有强制转换[-Wint-conversion]            temp [j] = modeString [i];

xiwangvsqiji 回答:将char连接到char *

除了注释中指出的错误(从[]的声明中删除temp)之外,您的代码中还有其他两个“错误”,如果您是尝试做的就是我的想法。 (我假设您要打印单个字符,直到找到,分隔符,然后在该点打印提取的字符串。)

首先,您需要将第一个j = 0语句移到for循环的 outside 外,这样它就不会每次都保持重置状态(除非找到逗号) )。第二(假设您想打印出最终的“提取物”,即使没有终止逗号,那么您还需要检查空终止符本身。

以下是我对 思考 所追求的建议(请随时纠正我的假设):

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

static void assignMode()
{
    char modeString[] = "SPI_LOOP,SPI_CPOL";
    char* temp = (char*)malloc(strlen(modeString)); /// Note: temp should not be an ARRAY of pointers - just one!
    int i,j = 0; /// Initialise j here - NOT inside the for loop!
    printf("Length of string a = %zd \n",strlen(modeString)); /// Use "%zd" for size_t!
    for (i = 0; i <= (int)strlen(modeString); i++) /// Always safer to convert size_t to int before comparison!
    {
    //  j = 0; /// See comment above!
        if (!(modeString[i] == ',') && modeString[i]) /// This also takes the end-of-string as a token delimiter
        {
            printf("Char: %c \n",modeString[i]);
            temp[j] = modeString[i];
            temp[j + 1] = '\0';
            j++;
        }
        else
        {
            j = 0;
            printf("temp: %s\n",temp);
        }
    }
    free(temp);/// Always FREE memory created with MALLOC when you're done with it!
}

int main()
{
    assignMode();
    return 0; /// Generally good practice to put this explicit return in main!
}

输出:

Length of string a = 17
Char : S
Char : P
Char : I
Char : _
Char : L
Char : O
Char : O
Char : P
temp : SPI_LOOP
Char : S
Char : P
Char : I
Char : _
Char : C
Char : P
Char : O
Char : L
temp : SPI_CPOL

随时要求进一步的澄清和/或解释。

,
  

我正在尝试连接/附加一个char *和char。

有几种选择。

  1. 假设您的字符串是可变的,并且基础数组具有足够的空间:

    char array[100] = "abracadabra";
    char suffix = '$';
    
    size_t alen = strlen(array);
    array[alen] = suffix;
    array[alen+1] = '\0';
    
  2. 假设您的字符串已分配,并且需要增长:

    char *array = malloc(12);
    strcpy(array,"abracadabra");
    char suffix = '$';
    
    size_t alen = strlen(array);
    char *tmp = realloc(array,alen + 2); // space for suffix and '\0'
    if (tmp) { array = tmp; }
    else { fprintf(stderr,"No memory.\n"); exit(EXIT_FAILURE); }
    
    array[alen] = suffix;
    array[alen+1] = '\0';
    
    // remember to free(array) later
    
  3. 假设原始字符串是只读的,需要复制到某个地方

    char *text = "abracadabra":
    char suffix = '$';
    
    char *tmp = malloc(strlen(text) + 2); // add space for suffix and '\0'
    sprintf(tmp,"%s%c",text,suffix);
    
    // remember to free(tmp) later
    
,

问题:

  1. 一个char数组可以是char * temp = ...,也可以是char temp [] = ...,但是您所做的(char * temp [])是char数组。当然,您需要的只是一个char数组。发生错误modetest.c: 36: 19: warning

  2. 在循环中,for(i=0; i<= max; i++)必须使用“ i

  3. 您不应在strlen (modeString)条件下调用for。相反,创建一个变量int max = strlen (modeString);,然后循环可以为for (i = 0; i <= max; i ++)。您现在拥有的方式,strlen会被不必要地调用多次。

  4. 您可以将所有modeString [i]减少到一个通话char c = modeString [i];,然后在需要的地方使用c

  5. 您应使用printf("temp: %s\n",temp)完成例程。

#include <stdio.h>

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

static void assignMode() {
    char modeString[] = "SPI_LOOP,SPI_CPOL";
    size_t max = strlen(modeString);
    char * temp = malloc(max + 2);
    int j = 0;
    printf("Length of string a = %zd\n",max);
    for (int i=0; i< max; i++) {
        char c = modeString[i];
        if(!(c == ',')) {
            printf("Char: %c \n",c);
            temp[j] = c;
            temp[j+1] = '\0';
            j++;
        } else {
            j = 0;
            printf("temp: %s\n",temp);
        }
    }
    printf("temp: %s\n",temp);
}

int main(int argc,const char * argv[]) {
    assignMode();
    return 0; }
本文链接:https://www.f2er.com/3165327.html

大家都在问