我目前陷入这个问题。
我有以下代码:
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。
我也在在线编译器和其他机器上尝试过,但结果仍然相同。我缺少什么?
被一个问题困扰了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)
你可以看到,我传递了一个参数temp给strlen,返回值len肯定是110。但接下来的语句if (len > 100)评估结果为 false!
系统:linux
CPU架构:32位ARM SOC:nt98566
请帮我!谢谢
我有测试的东西:
len给另一个int变量赋值,事情就会好起来。像下面这样例子:
char temp[100] = { 0 };
memset(&temp, 1, 110);
int len = …Run Code Online (Sandbox Code Playgroud) 我在这里不知所措.将在短时间内发布我的代码...只是它太长了,无法提取部分"令人不安"的代码.将expalin我的问题:我在一个结构数组中存储一个字符串(文件或目录的路径),{ char *path; size_t path_len}其中path是字符串path_en及其长度.在插入path_lenis 76时.从数组中提取strncpy字符串长度变为78或甚至数组中的字符串的简单strlen说77.
原始字符串长度小于77的所有其他情况都可以正常工作.
我很困惑.
此函数通过循环遍历字符数组并检查其中是否有任何字符与允许的字符列表不匹配来检查特殊字符.这个功能有什么问题?如果你能提供帮助,非常感谢!
假设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) 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从到int到size_t,但似乎并没有帮助.
我只是在一些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)
} …
我目前正在制作一个涉及为考试创建模板的计划.在我允许用户向考试添加问题的功能中,我需要确保仅使用存储其数据所需的内存.经过对各种输入函数(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代码如下:
char s="MIDSH"[3];
printf("%d\n",strlen(&s));
Run Code Online (Sandbox Code Playgroud)
运行的结果是2,这是错误的,因为char只是一个'S'.
有谁知道为什么以及如何解决这个问题?
#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语言,无法弄清楚为什么这段代码不起作用。我正在反转一个字符串,但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