简体   繁体   English

如何在编译时常量中使用函数

[英]How to use a function in a compile-time constant

I am trying to declare a random string as a variable that changes each run that everything can access however my knowledge of C is very limited. 我试图将随机字符串声明为变量,以更改每次运行都可以访问的运行,但是我对C的了解非常有限。

I've tried researching multiple websites and using tutorials but I cannot seem to explain it correctly. 我曾尝试研究多个网站并使用教程,但似乎无法正确解释。


// Declare example
#define DEST_SIZE 40

char *randstring(int length) {    
    char *string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
    size_t stringLen = 26*2+10+7;        
    char *randomString;

    randomString = malloc(sizeof(char) * (length +1));

    if (!randomString) {
        return (char*)0;
    }

    unsigned int key = 0;

    for (int n = 0;n < length;n++) {            
        key = rand() % stringLen;          
        randomString[n] = string[key];
    }

    randomString[length] = '\0';

    return randomString;
}
// char *randomHome = randstring(10);

// char * payloadPath = "~/Desktop/resign_temp_app/";

char dest[DEST_SIZE] = "~/Desktop/AppSign/";

char *randomHome = randstring(10);

char* plx = strcat(dest, randomHome);
char* plx2 = strcat(dest, "/");

const char * payloadPath = dest;


// Function Example
int rmTempAppPath(){
   char dest2[DEST_SIZE] = "rm -rf ";
   char *command = strcat(dest2, dest);
   int status = system(command);
   if (status != 0)
   {
       return -1;
   }
   return 0;
}

I was hoping that it would just work however I get the following error and warning messages 我希望它能正常工作,但是我收到以下错误和警告消息

resignCore.c:39:20: error: initializer element is not a compile-time constant
char *randomHome = randstring(10);
                   ^~~~~~~~~~~~~~
resignCore.c:41:13: error: initializer element is not a compile-time constant
char* plx = strcat(dest, randomHome);
            ^~~~~~~~~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/secure/_string.h:131:3: note: 
      expanded from macro 'strcat'
                __builtin___strcat_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
resignCore.c:42:14: error: initializer element is not a compile-time constant
char* plx2 = strcat(dest, "/");
             ^~~~~~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/secure/_string.h:131:3: note: 
      expanded from macro 'strcat'
                __builtin___strcat_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 errors generated.

I am really sorry for providing quite a bit of code and errors but I am not very good at explaining. 非常抱歉,我提供了很多代码和错误,但是我不太擅长解释。

Here is a modified version of your code to work as a simple example. 这是代码的修改版本,以作为一个简单示例。 Just for the future, it doesn't matter if your code is big. 只是为了将来,代码是否很大都没有关系。 Reduce the scope of the problem. 缩小问题范围。 I don't need your whole project source code, just give us a simple snippet that isolates the problem as much as possible. 我不需要您的整个项目源代码,只需给我们提供一个简单的代码段,即可尽可能地隔离问题。

Now the problem was that you had global variables that you try to define with non compile-time constants. 现在的问题是,您尝试使用非编译时常量定义全局变量。 So you initialize them with compile-time constants such as NULL and then define then in your main function before you call your other functions. 因此,您可以使用诸如NULL类的编译时常量对其进行初始化,然后在调用其他函数之前在主函数中进行定义。

strcat(dest, src) appends src to dest and the return value is dest as well. strcat(dest, src)src附加到dest ,返回值也为dest char *val = strcat(dest, src) is superfluous and error prone. char *val = strcat(dest, src)是多余的并且容易出错。 So just use strcat(dest,src) . 因此,只需使用strcat(dest,src)

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

#define DEST_SIZE 40

// Globals
char dest[DEST_SIZE] = "~/Desktop/AppSign/";
char *randomHome = NULL;

char *randstring(int length) {    
    const char * string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-#'?!";
    size_t stringLen = sizeof(string)/sizeof(char);        
    char *randomString = NULL;
    randomString = malloc(sizeof(char) * (length +1));
    if (!randomString) {
        return NULL;
    }

    for (int n = 0;n < length;n++) {            
        unsigned int key = rand() % stringLen;          
        randomString[n] = string[key];
    }

    randomString[length] = '\0';

    return randomString;
}

void printGlobal()
{
    printf("%s",dest);
}

int main()
{
    randomHome = randstring(10);
    strcat(dest, randomHome);
    printGlobal();
    strcat(dest, "/");
    printGlobal(); // print updated dest value
}

Disclaimer, there will be a memory leak if you don't delete randomString that you generate after usage. 免责声明,如果您不delete使用后生成的randomString,则会发生内存泄漏。

What I like to do sometimes, especially when I am testing new code or libraries out it create a mini project for instance a single file project. 有时我喜欢做的事情,特别是当我测试新代码或库时,会创建一个迷你项目,例如单个文件项目。 Then try to create a minimal working example. 然后尝试创建一个最小的工作示例。 As you can see, I didn't need the other 2 files to try and debug this problem. 如您所见,我不需要其他2个文件来尝试调试此问题。

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

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