这是我的第一篇文章。我一直在搜索,但找不到任何有助于解决问题的问题。当我尝试取消引用指针或在函数中应用指针数学时,事情并没有做他们应该做的事情。
例如:*(ptr + a)应该从技术上推进指向我设置的任何值的指针。相反,我只是得到一个错误,上面写着"请求会员' name'在一些不是结构或联合的东西中#34;这没有任何意义,因为我已经声明并将我的结构指针传递给函数。如果我使用:
" start_ptr-> name.last + a"风格,它编译但不排序任何东西。我知道第二个版本在指针数学方面也是不对的。
所以基本上,似乎错误的是编译,看似正确的不是,实际上也没有任何东西。
这是代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* Structure definition */
/* -------------------- */
struct split
{
char last[10];
char first[10];
};
struct info
{
struct split name;
char address[20];
char city[15];
char state[3];
long zip;
int age;
char gender;
}; /* end struct info */
void sort_them(struct info *, char); // sort prototype
int main(void)
{
/* Declare variables */
/* ----------------- */
struct info people[] =
{
{"Asimov", "Isaac", "15 Main St", "Worcestor", "MA", 01555, 23, 'M'},
{"Smith", "Jane", "17 Make Peace", "Wallham", "ND", 10102, 28, 'F'},
{"De Rippa", "Jack", "18 Able Way ", "Boston", "MA", 50503, 74, 'M'},
{"Cobb", "Jim", "55 Elm St", "Ware", "MO", 61555, 65, 'M'},
{"Kapone", "Al", "15 Morin Ave", "Idunno", "MN", 31333, 34, 'M'},
{"Seigel", "Myron", "44 Wing Blvd West", "Sandwich", "WA", 02537, 21, 'M'},
{"Thymes", "Mary", "88 Same Place", "Washington", "DC", 90555, 44, 'F'}
};
int bad_sort;
int num_people = sizeof(people) / sizeof(people[0]); //this will be the count of how many people there are!
char sort_order;
char big_name[22];
struct info *start_ptr = &people[0];
struct info *nums_ptr, *nums_end_ptr = &people[6];
char *big_ptr = &big_name[0];
printf ("\nWelcome to the People Structure Data Report Program\n");
do
{
bad_sort = 1;
printf ("\nEnter the sort order for the report: ");
printf ("\n(N=Name, A=Age, S=State, Z=Zip code ' '=no sort)\n ");
/* blank or return is allowed for no sorting of the data */
sort_order = getchar();
if(sort_order == '\n' || sort_order == ' ') break;
sort_order = toupper(sort_order);
if ((sort_order == 'N') || (sort_order == 'A') ||
(sort_order == 'S') || (sort_order == 'Z'))
bad_sort = 0;
else
printf("\nIncorrect Sort order selected, please re-enter: ");
} while (bad_sort == 1);
switch (sort_order)
{
case 'N':
printf("Sort by Name %i People.\n", num_people);
break;
case 'A':
printf("Sort by Age %i People.\n", num_people);
break;
case 'S':
printf("Sort by State %i People.\n", num_people);
break;
case 'Z':
printf("Sort by Zip %i People.\n", num_people);
break;
default:
printf("No Sort selected %i People.\n", num_people);
break;
} /* end cases */
if(sort_order != ' ')
sort_them (start_ptr, sort_order);
/* Print Report */
/* ------------ */
printf("\n\n The People Report\n\n");
printf ("\n\n%-20s%-20s %-15s %-5s %-6s %-3s %-6s",
"Name", "Address","City","State","Zip","Age","Gender");
printf ("%-20s%-20s %-15s %-5s %-6s %-3s %-6s\n",
"----", "------","----","-----","---","---","------");
for (nums_ptr = start_ptr; nums_ptr <= nums_end_ptr; nums_ptr++, start_ptr++)
{
strcpy(big_ptr, start_ptr->name.last);
strcat(big_ptr, ", ");
strcat(big_ptr,start_ptr->name.first);
printf ("%-20s%-20s %-15s %-4s%.5ld %-3i %c\n",
big_ptr,start_ptr->address,start_ptr->city,start_ptr->state,start_ptr->zip,
start_ptr->age,start_ptr->gender);
} /* end for loop */
printf("\n\n");
return 0;
} /* end main */
void sort_them(struct info *start_ptr, char sort_by)
{
int a,b;
struct info temp;
for(a = 0; a < 6; a++)
for(b = a + 1; b < 7; b++)
{
if( (sort_by == 'N' && strcmp(start_ptr->name.last + a, start_ptr->name.last + b) > 0)
|| (sort_by == 'A' && start_ptr->age + a > start_ptr->age + b )
|| (sort_by == 'S' && strcmp(start_ptr->state + a, start_ptr->state + b) > 0)
|| (sort_by == 'Z' && (start_ptr->zip + a) > (start_ptr->zip + b) )
)
{
temp = *(start_ptr + a);
*(start_ptr + a) = *(start_ptr + b);
*(start_ptr + b) = temp;
} /* end if */
} /* end inner for loop */
};
/* end of sort them
答案 0 :(得分:0)
我认为以下一行
strcmp( start_ptr->name.last + a, start_ptr->name.last + b) > 0
应该像下面那样改变
strcmp( (start_ptr + a)->name.last,
(start_ptr + b)->name.last
) > 0
以及其他比较。
答案 1 :(得分:0)
首先,没有理由不在C:
中使用数组表示法for(a = 0; a < 6; a++)
for(b = a + 1; b < 7; b++)
{
if( (sort_by == 'N' && strcmp(start_ptr[a].name.last, start_ptr[b].name.last) > 0)
|| (sort_by == 'A' && start_ptr[a].age > start_ptr[b].age )
|| (sort_by == 'S' && strcmp(start_ptr[a].state, start_ptr[b].state) > 0)
|| (sort_by == 'Z' && (start_ptr[a].zip) > (start_ptr[b].zip) )
)
{
temp = start_ptr[a];
start_ptr[a] = start_ptr[b];
start_ptr[b] = temp;
} /* end if */
表达式start_ptr->name.last + a
将start_ptr[0].name.last
添加到(ptr+a)
并且没有&#34;样式&#34;指针算术。
如果* ptr指向有效对象,则可以在ptr
之前完成指针运算,其中a*sizeof(*ptr)
由sizeof(type)
提前。否则* ptr会产生段错误(例如,对于ptr = NULL)。然后你可以在我们的示例中使用* ptr:sizeof(struct info)
的类型:*(ptr+a)
。
基本上ptr[a]
与(ptr+a)
相同,&ptr[a]
为->
。
如果您想使用(start_ptr+a)->name
运算符,则需要编写
var Person = mongoose.model('Person');
var people = [];
people[0] = new Person({id: 'someid'});
people[0].set('name', 'Mario');
people[1] = new Person({id: 'someid'});
people[1].set('name', 'Mario');
people[2] = new Person({id: 'someid'});
people[2].set('name', 'Mario');
var errors = [];
for(person in people){
people[person].save(function(err, done){
if(err) errors.push(err);
if (person === people.length){ yourCallbackFunction(errors){
if (errors.length!=0) console.log(errors);
//yourcode here
};
}
});
}
例如。