【问题标题】:C Language: Newb translating code from javascript to C, code includes malloc, strncpy, pointersC语言:Newb将代码从javascript翻译成C,代码包括malloc、strncpy、指针
【发布时间】:2012-07-10 20:29:26
【问题描述】:

我正在转换(从 javascript)一个程序,该程序将采用可变长度的字符串(但始终低于 100 个字符)并返回单个变量中字符串中包含的数据。这是我的代码的第一部分,显然,我是 C 和一般编程的新手。此代码用于代码的第一部分,但学习如何正确编码这将使我知道如何编码其余部分。

我需要:

  • 前 4 位数字存储为 'stringID'
  • 要存储为“myindicator”的第 5 位数字
  • 第 6 位到(指示符 + 6)位存储为“var1”

示例输入:

'12345678901234567890123'

示例输出:

  • 字符串 ID = 1234
  • myindicator = 5
  • var1 = 67890123456

当我运行程序时,它返回“字符串 ID:H>a”,然后程序崩溃。任何帮助,将不胜感激。不,这不是家庭作业。

int main()

{
char mystring[100];
char *stringID;
int nep;
int *myindicator;
char *var1;


nep = 0;
printf("Please enter your CODE\n");
scanf("%s", &mystring);

stringID = (char *)malloc(4 * sizeof(char));

if(NULL != stringID)
{

    strncpy(stringID, mystring, 4);
    stringID[4] = '\0';
    free(stringID);
    nep = nep +4;
    printf("stringID: %s\n",myindicator);
}


if(NULL != myindicator)
{
    strncpy(myindicator, (mystring+nep, 1);
    nep++; 
    myindicator = *myindicator - '0';
    printf("Indicator : %d\n",myindicator);
}

var1 = (char *)malloc((nep + 6) * sizeof(char));
if(NULL != var1)
{
    strncpy(var1, mystring+nep, (myindicator+nep+6));
    var1[myindicator+nep+6] = '\0';
    free(var1);

    printf("Var 1: %s", var1);

    nep = nep +myindicator+6;
}

getchar();
return 0;
}

【问题讨论】:

  • 建议将每个部分(你有三个)分解为单独的函数,并确保每个部分完全按照您希望它们单独工作的方式工作,然后努力让它们一起工作。
  • 您可能需要扩展 fails to work properly 以获得良好的回复。例如。你得到编译错误吗?链接错误?它会做你没想到的事情吗?当你不看的时候,它会在你的卫生纸上打印葛底斯堡地址吗?
  • “我的代码无法正常工作。”请做更多的研究并使用您的调试器来描述代码是如何失败的,并将其简化为应该工作但不能工作的最简单的事情。如果您希望人们理解您,您还必须包含示例输入和预期输出。

标签: c string pointers malloc strncpy


【解决方案1】:

我修复了一些东西,在 cmets 中找到它。但是你需要查看C语言手册...!

int main()
{
   char mystring[100];
   char *stringID;
   int nep;
   // Changed to integer, not pointer to int.
   int myindicator;
   char *var1;

   nep = 0;
   printf("Please enter your CODE\n");

   /*
       This scanf is a bad idea for the same reason for which, below, we take
       care to allocate memory enough for whatever we have to do.
       Should someone input 250 characters in a buffer of size 100, those 150
       extra characters would wreak havoc and possibly endanger the system.
   */
   // scanf("%s", &mystring);
   fgets(mystring, sizeof(mystring)-1, stdin);
   // fgets will read at most "sizeof(mystring)-1", that is, 99 bytes,
   // from "stdin" (STanDard INput), the same as scanf. But it will halt
   // when reaching the limit given. It's up to us to give a "real" limit
   // (nothing stops you from saying 15000 -- even if the true value is 100).

   // C strings are made of characters, terminated by a zero byte.
   // So you need 5 here, to store 4 characters
   stringID = (char *)malloc(5 * sizeof(char));

   if (NULL == stringID)
   {
       // Serious out of memory error: no sense going on.
       // fprintf(stderr, "Out of memory\n");
       abort();
   }

   strncpy(stringID, mystring, 4);
   stringID[4] = '\0';

   printf("ID: %s\n", stringID);

   free(stringID);

   nep = nep + 4;
   printf("NEP: %d\n", nep);

   // Now we want to decode the fifth digit.

   // I use '0' as character. So if the fifth digit is '0', '0'-'0' will give 0
   // and if it is '9', '9'-'0' will give 9 (the number).
   // The trick does not work with more than one digit, of course.
   myindicator = mystring[nep] - '0';

   // Had I wanted to read 3 digits, I would have had to copy them into a 
   // temporary buffer, add a zero in the fourth position, then run atol()
   // on the resulting buffer: atol("12345\0" /* A STRING */) = 12345 /* A NUMBER */;

   printf("VLI : %d\n", myindicator);

   // Copy "myindicator" bytes, so alloc myindicator+1 chars
   var1 = (char *)malloc((myindicator + 1) * sizeof(char));

   // Check that var1 is not null and abort if it is
   if (NULL == var1)
        abort();

   strncpy(var1, mystring + 6, myindicator);
   var1[myindicator+1] = '\0';

   // Moved this printf before the free. See why below.
   printf("Prefix : %s\n", var1);

   // NEVER use a variable after you freed it!!!
   // it might APPEAR to work, but will stab you in the back the first chance it gets.
   // Good if paranoid habit: null a var as soon as you've freed it.
   free(var1); var1 = NULL;

   getchar(); 
   return 0;
}

【讨论】:

  • 我猜他会逐字运行你的代码。请将对 scanf() 的调用更改为缓冲 IO。当孩子第一次接触终端时,他的“mystring”数组就会溢出。
  • 格式有小错误。您需要在int main 及其左大括号{ 前面添加一个空格。
【解决方案2】:

你为什么要释放你的数组?从堆中释放它们之后,您正在引用它们。

您的代码将在这些地方出现段错误:

  1. 你在哪里分配myindicator

    strncpy(myindicator, (mystring+nep, 1); // 这里会出现段错误。

  2. 免费(var1);

    printf("前缀:%s", var1); // 再次出现段错误

  3. 又来了

    strncpy(var1, mystring+nep, (myindicator+nep+6)) //你的mystring在哪里?

  4. 通过scanf() 获取字符串输入是非常可怕的想法。使用像fgets() 这样的缓冲IO。

  5. 您将mystring 暴露于缓冲区溢出。谁在阻止用户输入 120 字节的字符串?我可以通过仔细的跳转指令将您的堆栈写入我的恶意代码。

【讨论】:

    猜你喜欢
    • 2015-06-25
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2014-01-06
    相关资源
    最近更新 更多