硬件工程师的C程序,很少有编码背景

硬件工程师的C程序,很少有编码背景,c,C,我试图调试我编写的这个程序,但我得到的只是垃圾值 /* * Assignment.c * * Created on: 18-Aug-2017 * Author: sumeetpatil */ #include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdio.h> char rotateArray(char str[], int degree, i

我试图调试我编写的这个程序,但我得到的只是垃圾值

/*
 *  Assignment.c
 *
 *  Created on: 18-Aug-2017
 *  Author: sumeetpatil
 */

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

char rotateArray(char str[], int degree, int length) {
    char temp[length];
    for (int i = 0; i < length + 1; i++) {
        str[i] = str[i] +  &degree;
        str[i] = temp[i];
        //printf ("%c", str[i]);
    }
    return &str;
}

int main() {
    int length;
    int degree;
    char encryptArr[50];

    printf("Enter the value of degree: ", "\n");

    scanf("%d", &degree, "\n");

    printf("Enter the string array you want to encrypt: ", "\n");

    fgets(encryptArr, 100, stdin);

    scanf("%[^\n]%*c", &encryptArr);

    length = strlen(encryptArr);

    printf("The character string is: %s\n", encryptArr, "\n");

    printf("The size of character array is %u\n", length);

    rotateArray(encryptArr, degree, length);

    printf("%s\n", rotateArray);
}

请让我知道这个程序出了什么问题。

让我们从什么问题开始

scanf ("%[^\n]%*c", &encryptArr); //Wrong
scanf ("%[^\n]%*c", &encryptArr[0]); //OK
scanf ("%[^\n]%*c", encryptArr); //OK
现在你的旋转函数

char rotateArray(char str[], int degree, int length) {
    char temp[length]; //Declared on stack, values are not known, UB
    for(int i=0; i < length + 1; i++) { //Why +1? Forces UB in later stage
        str[i] = str[i]+ &degree; //Degree is variable on stack, adding & is just getting address from stack
        str[i] = temp[i]; //Assigning temp value with undefined value
        //printf ("%c", str[i]); //Printing undefined value
        }
    return &str; //You should get at least warning here.
    return str[0]; //Better
}

不能仅将函数名打印为字符串。这在C中是未定义的行为。

让我们从什么不好开始

scanf ("%[^\n]%*c", &encryptArr); //Wrong
scanf ("%[^\n]%*c", &encryptArr[0]); //OK
scanf ("%[^\n]%*c", encryptArr); //OK
现在你的旋转函数

char rotateArray(char str[], int degree, int length) {
    char temp[length]; //Declared on stack, values are not known, UB
    for(int i=0; i < length + 1; i++) { //Why +1? Forces UB in later stage
        str[i] = str[i]+ &degree; //Degree is variable on stack, adding & is just getting address from stack
        str[i] = temp[i]; //Assigning temp value with undefined value
        //printf ("%c", str[i]); //Printing undefined value
        }
    return &str; //You should get at least warning here.
    return str[0]; //Better
}

不能仅将函数名打印为字符串。这是C中未定义的行为。

您的代码包含一些错误的行为

我猜你可能来自另一种语言,并试图学习C语言

好吧,让我们看看你在代码中犯了什么样的错误


1.
2. 在主体中,几乎每个
printf
scanf
都在后面插入了
\n
。我再次猜测,这种编码风格可能受到另一种编程语言的影响?:)

实际上,在C中,除非在格式字符串处放置了说明符,否则不必将
\n
作为新参数。因此,您可以一起删除
,“\n”

/* printf("Enter the value of degree: ", "\n"); */
printf("Enter the value of degree: ");

/* scanf("%d",&degree, "\n"); */
scanf("%d",&degree);

/* printf("Enter the string array you want to encrypt: ", "\n"); */
printf("Enter the string array you want to encrypt: ");

/* printf("The character string is: %s\n", encryptArr, "\n"); */
printf("The character string is: %s\n", encryptArr);
printf
&
scanf
对于一个新的C程序员来说有点像野兽。请查看更多讨论这些问题的资源


3. 最后,在你身体的末端

printf("%s\n", rotateArray);
这有点胡说八道,因为您正试图打印函数名。好吧,我猜也许你认为它会打印函数的返回值,对吗

实际上,根据我前面在顶部告诉您的,作为
str
参数传入函数的任何字符串数组都有点像引用,因此当您在
rotatarray
主体内修改
str
时,它也会“影响”主函数的值

所以你可以把它改成这样,而不是错误的代码

/* printf("%s\n", rotateArray); */
printf("%s\n", encryptArr);

因此,最终修改的代码为:

/*
 *  Assignment.c
 *
 *  Created on: 18-Aug-2017
 *  Author: sumeetpatil
 */

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

/* char rotateArray(char str[], int degree, int length) { */
void rotateArray(char str[], int degree, int length) {

    /* char temp[length]; */

    for(int i=0; i < length +1; i++)
    {
        str[i] = str[i]+ degree;
        /* str[i] = temp[i]; */
        //printf ("%c", str[i]);
    }
    /* return &str; */
}

int main(){

    int length;
    int degree;
    char encryptArr[50];

    /* printf("Enter the value of degree: ", "\n"); */
    printf("Enter the value of degree: ");
    fflush(stdout);

    /* scanf("%d",&degree, "\n"); */
    scanf("%d",&degree);

    /* printf("Enter the string array you want to encrypt: ", "\n"); */
    printf("Enter the string array you want to encrypt: ");
    fflush(stdout);

    fgets(encryptArr, 100, stdin);

    scanf ("%[^\n]%*c", &encryptArr);

    length = strlen(encryptArr);

    /* printf("The character string is: %s\n", encryptArr, "\n"); */
    printf("The character string is: %s\n", encryptArr);

    printf("The size of character array is %u\n", length);

    rotateArray(encryptArr, degree, length);

    /* printf("%s\n", rotateArray); */
    printf("%s\n", encryptArr);

}
/*
*作业c
*
*创建日期:2017年8月18日
*作者:SumeetPail
*/
#包括
#包括
#包括
#包括
/*char rotatarray(char str[],整数度,整数长度){*/
void rotatarray(字符str[],整数度,整数长度){
/*字符温度[长度]*/
对于(int i=0;i

奖金 你在这里想要实现的就是所谓的

您的实现是错误的,因为它没有正确处理“字母溢出”。我的意思是,当您的代码超过
z
时,它不会再次将自身旋转到
a
。有许多方法可以实现这一点,可以使用余数运算符
%
或使用
if-else
语句检查字母h由于溢出


试着看看这个例子。

您的代码包含一些错误的行为

我猜你可能来自另一种语言,并试图学习C语言

好吧,让我们看看你在代码中犯了什么样的错误


1.
2. 在主体中,几乎在每一个
printf
scanf
上,您都在后面插入了
\n
。我再次猜测,这种编码风格可能受到另一种编程语言的影响:)

实际上,在C语言中,除非在格式字符串中添加了说明符,否则不必将
\n
作为新参数。因此,您可以删除
,“\n”

/* printf("Enter the value of degree: ", "\n"); */
printf("Enter the value of degree: ");

/* scanf("%d",&degree, "\n"); */
scanf("%d",&degree);

/* printf("Enter the string array you want to encrypt: ", "\n"); */
printf("Enter the string array you want to encrypt: ");

/* printf("The character string is: %s\n", encryptArr, "\n"); */
printf("The character string is: %s\n", encryptArr);
printf
&
scanf
对于一个新的C程序员来说有点像野兽。请看更多讨论这些问题的参考资料


3. 最后,在你身体的末端

printf("%s\n", rotateArray);
这有点胡说八道,因为您正在尝试打印函数名。好吧,我再次猜测,您可能认为它会打印函数的返回值,对吗

实际上,根据我前面在顶部告诉您的,作为
str
参数传入函数的任何字符串数组都有点像引用,因此当您在
rotatarray
主体内修改
str
时,它也会“影响”主函数的值

所以你可以把它改成这样,而不是错误的代码

/* printf("%s\n", rotateArray); */
printf("%s\n", encryptArr);

因此,最终修改的代码为:

/*
 *  Assignment.c
 *
 *  Created on: 18-Aug-2017
 *  Author: sumeetpatil
 */

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

/* char rotateArray(char str[], int degree, int length) { */
void rotateArray(char str[], int degree, int length) {

    /* char temp[length]; */

    for(int i=0; i < length +1; i++)
    {
        str[i] = str[i]+ degree;
        /* str[i] = temp[i]; */
        //printf ("%c", str[i]);
    }
    /* return &str; */
}

int main(){

    int length;
    int degree;
    char encryptArr[50];

    /* printf("Enter the value of degree: ", "\n"); */
    printf("Enter the value of degree: ");
    fflush(stdout);

    /* scanf("%d",&degree, "\n"); */
    scanf("%d",&degree);

    /* printf("Enter the string array you want to encrypt: ", "\n"); */
    printf("Enter the string array you want to encrypt: ");
    fflush(stdout);

    fgets(encryptArr, 100, stdin);

    scanf ("%[^\n]%*c", &encryptArr);

    length = strlen(encryptArr);

    /* printf("The character string is: %s\n", encryptArr, "\n"); */
    printf("The character string is: %s\n", encryptArr);

    printf("The size of character array is %u\n", length);

    rotateArray(encryptArr, degree, length);

    /* printf("%s\n", rotateArray); */
    printf("%s\n", encryptArr);

}
/*
*作业c
*
*创建日期:2017年8月18日
*作者:SumeetPail
*/
#包括
#包括
#包括
#包括
/*char rotatarray(char str[],整数度,整数长度){*/
void rotatarray(字符str[],整数度,整数长度){
/*字符温度[长度]*/
对于(int i=0;iint main(void) {
    int length;
    int degree;
    char encryptArr[50];

    printf("Enter the value of degree: ");
    if (scanf("%d", &degree) != 1)
        return 1;

    printf("Enter the string array you want to encrypt: ");
    if (!fgets(encryptArr, sizeof(encryptArr), stdin))
        return 1;

    length = strlen(encryptArr);
    printf("The character string is: %s\n", encryptArr);
    printf("The length of character string is %d\n", length);

    rotateArray(encryptArr, degree, length);
    printf("%s\n", encryptArr);

    return 0;
}
#include <stdio.h>
#include <string.h>
char *rotateArray(char *str, int degree, int length) {
    for (int i = 0; i < length; i++) {
        char c = str[i];
        if (c >= 'a' && c <= 'z') {
            str[i] = 'a' + (c - 'a' + degree) % 26;
        } else
        if (c >= 'A' && c <= 'Z') {
            str[i] = 'A' + (c - 'A' + degree) % 26;
        }
    }
    return str;
}