标签: strlen

了解 Strlen 应用于带有 char * 转换的 int 数组

我目前陷入这个问题。

我有以下代码:

    int v[10] = {-1, 1000, 2};
    
    int a;
    
    a = strlen((char *)v+1);
    
    printf("strlen result for (char *) v+1: %d\n", a);
Run Code Online (Sandbox Code Playgroud)

我无法理解为什么在小端“机器”上最终结果总是 5。根据 strlen 文档,在这种情况下 char * 转换应返回 4。

我也在在线编译器和其他机器上尝试过,但结果仍然相同。我缺少什么?

c pointers strlen

0
推荐指数
1
解决办法
140
查看次数

当strlen的参数是非零结尾的字符串时,其返回值在IF语句中不安全?

被一个问题困扰了strlen很久,代码如下:

   char stack_protect[1000] = {0};
    char temp[100] = { 0 };
    memset(&temp, 1, 110);
    int len = strlen(temp);
    
    printf("test strlen of temp %d \n", len);
    if (len > 100)
    {
        xxxx;
    }
    else
    {
        xxxx;
    }

   char stack_protect2[1000] = {0};
Run Code Online (Sandbox Code Playgroud)

你可以看到,我传递了一个参数tempstrlen,返回值len肯定是110。但接下来的语句if (len > 100)评估结果为 false!

系统:linux
CPU架构:32位ARM SOC:nt98566

请帮我!谢谢

我有测试的东西:

  1. 如果你len给另一个int变量赋值,事情就会好起来。像下面这样

例子:

    char temp[100] = { 0 };
    memset(&temp, 1, 110);
    int len = …
Run Code Online (Sandbox Code Playgroud)

c linux strlen

0
推荐指数
1
解决办法
133
查看次数

字符串长度为76的问题

我在这里不知所措.将在短时间内发布我的代码...只是它太长了,无法提取部分"令人不安"的代码.将expalin我的问题:我在一个结构数组中存储一个字符串(文件或目录的路径),{ char *path; size_t path_len}其中path是字符串path_en及其长度.在插入path_lenis 76时.从数组中提取strncpy字符串长度变为78或甚至数组中的字符串的简单strlen说77.

原始字符串长度小于77的所有其他情况都可以正常工作.

我很困惑.

c strlen strncpy

-1
推荐指数
1
解决办法
90
查看次数

为什么这个功能不起作用?Strlen,用于循环的数组

此函数通过循环遍历字符数组并检查其中是否有任何字符与允许的字符列表不匹配来检查特殊字符.这个功能有什么问题?如果你能提供帮助,非常感谢!

假设str_split_array($ stringPassed); 完美的工作(因为我99%确定它确实如此,我在各种其他功能中使用它)

    // returns true if a string has special characters
// $stringPassed = input string to split and check
// $checkWhiteSpace = check for whitespace, true or false
function hasSpecialCharacters($stringPassed, $checkWhiteSpace = true) {
    // Array of characters ALLOWED
    $allowedCharacters = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0); …
Run Code Online (Sandbox Code Playgroud)

php arrays strlen

-1
推荐指数
1
解决办法
255
查看次数

strlen()的另一个问题

int main(void)
{
    int i;
    for (i = 0; i < strlen("word"); i++ );
    {
        printf("_");
        printf(" ");
    }
return 0;
}
Run Code Online (Sandbox Code Playgroud)

我似乎再次遇到strlen()的问题."_ _ _ _ "在这种情况下,for循环应该打印,因为单词中有四个字母.我猜它是strlen()返回一个size_t值.如何解决这个问题?我试图改变i从到intsize_t,但似乎并没有帮助.

c strlen

-1
推荐指数
1
解决办法
263
查看次数

Strlen无法正常工作

我只是在一些PHP函数上体验来控制一些表单字段.

我刚刚构建了一个真正的小形式,看看如何处理OOP表单,但我遇到了strlen命令的奇怪行为.

这是代码:

阶级形式及其微小的控制

class Form {

public $nome = '';

public function __construct() {
    if(isset($_POST['nome'])){
    $this->nome = $_POST['nome'];
    }
}
public function body() {

    return "<form action=$_SERVER[PHP_SELF] method=post>
           <input type=text name=nome value=\"$this->nome\">
           <input type=submit value=INVIA><br>";
    }

  }

class check {

public $nome;

public function __construct() {
    $this->nome = $_POST['nome'];
}

public function controllo() {

    if ( !empty($this->nome) && strlen($this->nome >= 3) ) {
        echo "$this->nome è un nome valido";
        return TRUE;
    } else {
        return FALSE;
    }
}
Run Code Online (Sandbox Code Playgroud)

} …

php string match strlen

-1
推荐指数
1
解决办法
413
查看次数

为什么在为字符数组的strlen分配空间时添加1?

我目前正在制作一个涉及为考​​试创建模板的计划.在我允许用户向考试添加问题的功能中,我需要确保仅使用存储其数据所需的内存.经过对各种输入函数(getc,scanf等)之间的差异的大量研究,我已经设法这样做了,我的程序似乎正在运行,但我关注一件事.这是我的函数的代码,我在相关的行上发表了评论:

int AddQuestion(){

Question* newQ = NULL;
char tempQuestion[500];
char* newQuestion;

if(exam.phead == NULL){
    exam.phead = (Question*)malloc(sizeof(Question));
}
else{
    newQ = (Question*)malloc(sizeof(Question));
    newQ->pNext = exam.phead;
    exam.phead = newQ;
}

while(getchar() != '\n');

puts("Add a new question.\n"
     "Please enter the question text below:");
fgets(tempQuestion, 500, stdin);

newQuestion = (char*)malloc(strlen(tempQuestion) + 1); /*Here is where I get confused*/
strcpy(newQuestion, tempQuestion);

fputs(newQuestion, stdout);
puts("Done!");

return 0;
}
Run Code Online (Sandbox Code Playgroud)

令我感到困惑的是,我尝试运行相同的代码,但只需稍加修改即可测试幕后发生的事情.我尝试从我malloc那里删除+ 1 ,我放在那里因为strlen只计算但不包括终止字符,我假设我想要包含终止字符.那仍然没有顺利.所以我尝试运行它,但是使用-1而不是印象,这样做会删除终止字符之前的任何内容(换行符,正确吗?).不过,它在不同的行上显示了所有内容.所以现在我有些困惑,并怀疑我对字符数组如何工作的了解.有人可以帮助澄清这里发生的事情,或者可能为我提供一个资源,可以更详细地解释这一切吗?

c arrays malloc strlen

-1
推荐指数
1
解决办法
1364
查看次数

为什么C函数strlen()返回一个错误的char长度?

我的C代码如下:

char s="MIDSH"[3];
printf("%d\n",strlen(&s));
Run Code Online (Sandbox Code Playgroud)

运行的结果是2,这是错误的,因为char只是一个'S'.

有谁知道为什么以及如何解决这个问题?

c string strlen

-1
推荐指数
3
解决办法
1923
查看次数

malloc,sizeof和strlen函数可能存在冲突吗?

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

typedef struct _person
{
    char *fname;
    char *lname;
    bool isavailable;
}Person;


Person *getPersonInstance(void)
{
    Person *newPerson = (Person*) malloc(sizeof(Person));
    if(newPerson == NULL)
        return NULL;
    return newPerson;
}

void initializePerson(Person *person, char *fname, char *lname, bool isavailable)
{
    person->fname = (char*) malloc(strlen(fname)+1);
  /*problematic behaviour if i write: person->fname = (char*) malloc (sizeof(strlen(fname)+1)); */

    person->lname = (char*) malloc(strlen(lname)+1);
 /*problematic behaviour if i write: person->lname = (char*) malloc (sizeof(strlen(lname)+1)); */

    strcpy(person->fname,fname);
    strcpy(person->lname,lname);
    person->isavailable = isavailable; …
Run Code Online (Sandbox Code Playgroud)

c malloc sizeof strlen

-1
推荐指数
1
解决办法
150
查看次数

无法弄清楚为什么strlen破坏了C中的字符数组

我正在学习C语言,无法弄清楚为什么这段代码不起作用。我正在反转一个字符串,但strlen似乎正在破坏输入。

我知道scanf可能很麻烦,所以我尝试用fgets替换scanf(我现在还不知道),我得到的输出完全相同。

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

int main()
{
    char inputstring[] = "";
    char outputstring[] = "";
    unsigned short int count = 0;

    printf("Enter a string: ");
    scanf("%s", inputstring);

    count = strlen(inputstring) - 1;

    printf("%s", inputstring);

    for(int i = 0;i <= count;i++)
    {
        outputstring[count-i] = inputstring[i];
    }

    printf("%s", outputstring);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我编译并执行代码时,我得到:

输入字符串:hello h

因此printf显示一个字符,然后显示垃圾内容。

如果我注释掉粗线,则printf正确显示:

输入一个字符串:hello hello

c scanf strlen

-1
推荐指数
1
解决办法
80
查看次数

标签 统计

strlen ×10

c ×8

arrays ×2

malloc ×2

php ×2

string ×2

linux ×1

match ×1

pointers ×1

scanf ×1

sizeof ×1

strncpy ×1