小编Bob*_*bby的帖子

将变量分配给字符串

我是学习结构的初学者.我的代码有点问题.我知道这不是分配字符串的方法.有人可以告诉我如何?该网站教会我这样做.

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

#define MAX_PERSON 50
#define NAME_LENGTH 30
#define ADDR_LENGTH 60

struct player {

    char name [NAME_LENGTH];
    char address [ADDR_LENGTH];     
    int salary;

};

int main (int argc, char *argv[]){

   struct player singleTeams;
   struct player multipleTeams[MAX_PERSON];

   singleTeams.name = "David";
   multipleTeams[20].name = "Robin";

   printf("Person on the single team is %s\n", singleTeams.name);
   printf("Person on the multiple team is %s\n", multipleTeams[20].name);


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

我的错误是在这一行

   singleTeams.name = "David";
   multipleTeams[20].name = "Robin";
Run Code Online (Sandbox Code Playgroud)

c++ string struct

4
推荐指数
1
解决办法
149
查看次数

C编程简单指针

我是学习指针的初学者.这是我的代码.(注意:我仍然试图绕过指针,所以我的代码不干净.)

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

int main (int argc, char *argv[]){

int a = 1;
char b = 's';
double c = 3.14;
int *ptra;
int *ptrb;
int *ptrc;

ptra = &a;
ptrb = &b;
ptrc = &c;

printf("I initialised int as %d and char as %c and double as %.2f\n", a, b, c);
printf("The address of A is %p and the contents of A is %d\n", ptra, *ptra);
printf("The address of B is %p and the contents of B …
Run Code Online (Sandbox Code Playgroud)

c pointers

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

C中的指针(将地址传递给函数)

我正试图解决这个问题.问题是,*"swap_nums似乎有效,但不是swap_pointers.修复它."*顺便说一下,我是初学者:)

我相信我可以自己解决这个问题但问题是我在理解C中的一些编程概念时遇到了一些困难.这里我展示了需要编辑的给定代码.到目前为止,我将展示我的思维过程.请注意:我想要一些提示,而不是完整的解决方案.:-)

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

void swap_nums(int *x, int *y);
void swap_pointers (char *x, char *y);

int main (int argc, char *argv[]){

   int a = 3, b = 4;
   char *s1, *s2;
   swap_nums(&a, &b);
   printf("a is %d\n", a);
   printf("b is %d\n", b);

   s1 = "I should print second";
   s2 = "I should print first";

   swap_pointers(s1, s2);
   printf("s1 is %s\n", s1);
   printf("s2 is %s\n", s2);

   return EXIT_SUCCESS; } 

void swap_nums(int *x, int *y){

   int temp;
   temp = *x;
   *x = …
Run Code Online (Sandbox Code Playgroud)

c pointers

2
推荐指数
1
解决办法
5254
查看次数

指数值不会改变

我正在尝试实现线性搜索功能来搜索用户"输入"的特定数字,例如用户想要搜索给定数组中的数字3.该函数应返回索引值2.但无论输入什么输入,我的代码都返回6.我怀疑我的main函数有问题(可能在我使用for循环时,i的值固定为6?).有任何想法吗?

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


#define numberOfElements 6
#define NOT_FOUND -1

int linearSearch (int *myArray, int key, int i);

int main (int argc, char *argv[]){

   int i, key, myArray[] = {1, 2, 3, 4, 5, 6};
   printf("Input: ");
   for (i = 0; i < numberOfElements; i++){
      printf("%d ", myArray[i]);
   }
   printf("\n");
   printf("Please enter a number you wish to search for: ");
   scanf("%d", &key);
   linearSearch (myArray, key, i);
   printf("The number %d is at index %d\n", key, i);

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

c arrays

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

标签 统计

c ×3

pointers ×2

arrays ×1

c++ ×1

string ×1

struct ×1