我听说使用正则表达式验证电子邮件地址是一件坏事,它实际上可能会造成伤害.这是为什么?我认为验证数据永远不会是件坏事.如果您正确执行验证,可能不必要,但绝不是坏事.你能解释一下为什么这是对还是错?如果它可能造成伤害,请举个例子.
是否可以使用for循环在C中获取所有基本数据类型的大小?例如,我们可以这样做吗?
#include <datatypes.h> /* or something else which defines get_data_types() */
#include <stdio.h>
int main() {
for (int x = 0; x < len(get_data_types()); x++) {
printf("Size of %s is %d", get_data_types()[x], sizeof(get_data_types()[x]));
}
}
Run Code Online (Sandbox Code Playgroud)
我可以通过替换for循环并为int,long等编写单独的语句来枚举所有数据类型.但是,我想知道是否有可能有一个for循环来做同样的事情?
基本上,我试图避免以下情况:
#include <stdio.h>
int main() {
printf("Size of int is %d", sizeof(int);
printf("Size of unsigned int is %d", sizeof(unsigned int);
printf("Size of long is %d", sizeof(long);
/* etc. */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
数据类型的兴趣 - ,char,unsigned char, …
#include <stdio.h>
int main()
{
int a = "Hi";
char b = 'F';
int c = a + b;
printf("%d",a); /* (1) */
printf("%d",c); /* (2) */
}
Run Code Online (Sandbox Code Playgroud)
为什么指令(1)的输出是18537?它是如何以ANSI标准存储值的
指令(2)很清楚,因为我们基本上增加了70到18537,它给出了18607
有人可以根据字符串详细说明值是如何存储在整数变量中的吗?
任务是创建一个功能
int* pairSumSearch(int* data, int numEls, int pairSum)
Run Code Online (Sandbox Code Playgroud)
如果ints'数据',则在列表的第一个numEls内搜索,以找到与'pairSum'相加的两个相邻整数,并返回指向两个整数中第一个的位置的指针.如果没有两个int总和为pairum,那么该函数应该返回一个空指针.我认为我的所有代码都是正确的,除了指针部分.
int* pairSumSearch(int* data, int numEls, int pairSum)
{
int* point = NULL;
int checker = data[0];
for (int i = 1; i < numEls-1; i++) {
if (checker + data[i] == pairSum) {
*point = i-1;
break;
} else {
checker = data[i];
}
}
return point;
}
Run Code Online (Sandbox Code Playgroud)
测试用例是
int main(void)
{
int data[] = {1, 2, 3, 4, 5, 6, 7, 10, 9, 10};
int* p = pairSumSearch(data, 10, 19); …Run Code Online (Sandbox Code Playgroud) 我认为输出应该是 10 10,但它是 10 1 并且不知道为什么。
我尝试分配 arr[0]=55 ,它使我成为 55 idk 为什么会这样。很高兴得到解释
void foo(int arr[])
{
int i=10;
arr=&i;
printf("%d",arr[0]);
}
int main()
{
int a[4]={1,2,3,4};
foo(a);
printf("%d",a[0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的想法:1010 实际 o/p= 101
我编写了一个不可知函数,char以十六进制格式打印字符串。
void print_char2hex(void* cp) {
const char *arr = (char *) cp;
size_t i;
for(i = 0; i < strlen( arr ); i++) {
printf("%02X ", 0xFF & arr[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么以下代码无法编译?
void print_char2hex(const void *cp) {
size_t i;
for (i = 0; i < strlen( * (const char *) cp ); i++) {
printf("%02X ", 0xFF & cp[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
passing argument 1 of strlen makes pointer from integer without a cast [-Wint-conversion]
Run Code Online (Sandbox Code Playgroud) 我是C开发的新手,我想知道是否有任何方法可以像下面的Java代码那样在C函数中返回任何类型:
public static Object returnAnyType(Object anyType){
//example useless code
return anyType;
}
public static void main(String[] args) {
System.out.println(returnAnyType('c'));
// outs 'c'
}
Run Code Online (Sandbox Code Playgroud)
我知道有一种方法可以返回任何类型的指针作为voidC中的指针:
void* returnAny(void* anyType){
//example useless code
return anyType;
}
void main(){
char ch = 'c';
printf("%c\n", *(char*)returnAny(&ch));
// outs 'c'
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否有可能在C中具有相同的上述Java代码,而不必在返回类型和参数中使用void指针。到目前为止,我已经尝试使用void返回类型,但是我的编译器不接受没有返回类型的return语句。
我看到了以下代码:
char *str;
// Some code
if (! str || ! *str)
return str;
Run Code Online (Sandbox Code Playgroud)
为什么需要检查! *str?还if (! str)不够吗
我想输出数据看起来像这样
input : 1999.99
output: 999.99
Run Code Online (Sandbox Code Playgroud)
我想去掉更重要的数字。
我已经使用过这样的 C 格式
%.2f ==> 10.44, 23.23, 5.67
Run Code Online (Sandbox Code Playgroud)
现在我需要在小数点前切割。
有什么解决办法吗?
我知道这char[]比String密码更可取,因为它String是不可变的。但是我一直无法弄清楚如何将它传递给准备好的语句。考虑这个代码:
Connection con = MyDBManager.connect();
PreparedStatement stm = con.prepareStatement(
"INSERT INTO my_table(username, password) VALUES(? ?)");
String username = "Jonny";
stm.setString(username);
char[] password = new char[] {'a','b','c','d'};
stm.SetString(password.toString());
Run Code Online (Sandbox Code Playgroud)
它有效,但我怀疑调用会password.toString()破坏使用 a 的全部目的char[],那么我该怎么办?
是的,我知道密码不应该以明文形式存储。密码是散列的,但我仍然想使用char[]
这似乎有效,而且我还将数据库中的字段从 更改text为bytea并且(我认为)理论上应该提供与char[]预期相同的好处,但我强烈感到我做得不对:
byte[] password = new byte[] {'a','b','c','d'};
stm.SetBytes(password);
Run Code Online (Sandbox Code Playgroud)
也许我的方法完全错误,但我的要求是密码哈希永远不应存在于不可变对象中。我对将其发送到数据库的其他方式持开放态度PreparedStatement
我从@rzwitserloot 的回答中学到了很多东西,但不幸的是它没有帮助。永远不要在不可变对象中使用密码哈希是不可协商的要求。
c ×8
pointers ×2
arrays ×1
dereference ×1
double ×1
email ×1
format ×1
gcc ×1
java ×1
list ×1
null ×1
null-pointer ×1
numbers ×1
passwords ×1
postgresql ×1
regex ×1
sizeof ×1
types ×1
validation ×1