Question:
help with c programming !?
wolfhead321
2011-04-07 17:15:33 UTC
/***************************************…
Exercise 1.8
A. This program does not compile.
Explain why, and fix it.
B. Then rewrite the swap function using
good style.

Name:
Date:
/*************************************…
#include
#include
struct book{
char ISBN[15];
char *author;
int year;
int nopages;
};

int main(void)
{
void printList(struct book list[]);
void swap ( struct book a, struct book b );
struct book list[10] =
{
{"0170088219", "Ron House", 1994, 568},
{"0534951406", "John W. Perry", 1998, 304},
{"0131103628", "Dennis M.Ritchie", 1988, 272},
{"", NULL, 0, 0},
};

printList(list);
swap(list[0], list[1]);
printList(list);

return 0;
}

/*************************************…
Prints a list of book structures
*/
void printList(struct book list[])
{
int i;

putchar('\n');
for (i = 0; list[i].author != NULL; i++)
{
printf( "%10s %-20s %d %d\n", (*(list + i)).ISBN,
(*(list + i)).author,
(*(list + i)).year,
(*(list + i)).nopages);
}
return;
}

/*************************************…
swap two book structures
*/
void swap ( struct book a, struct book b )
{
struct book temp;

memcpy(temp.ISBN, a.ISBN, sizeof(temp.ISBN));
memcpy(a.ISBN,b.ISBN, sizeof(a.ISBN));
memcpy(b.ISBN, temp.ISBN, sizeof(b.ISBN));

temp.author = a.author;
a.author = b.author;
b.author = temp.author;

temp.year = a.year;
a.year = b.year;
b.year = temp.year;

temp.nopages = a.nopages;
a.nopages = b.nopages;
b.nopages = temp.nopages;

return;
}

why doesn't my swap function work when the compiler didn't show any error
Four answers:
juliepelletier
2011-04-07 17:26:27 UTC
The compiler can not tell you if you know what you`re doing or not.



Here are a few things I noticed:

- swap() takes doesn`t take its parms by reference, so it won`t affect the structures outside of its scope.

- Why do you copy your structs element by element instead of copying the whole struct?
Dan
2011-04-07 17:27:00 UTC
Your swap() function should take pointers to the book structures: (struct book *a, struct book *b)



The way you have it written, you are passing the structures by value. That means a copy of the data is made on the stack. The swap function then makes all the changes on the stack. When the swap() function returns, the stack is popped and nothing has happened in the calling function.



If you pass pointers to the structures in your calling function, the swap() function will operate on the right area in the memory.



There may be other errors, but this is the major one.
anonymous
2017-02-20 19:27:41 UTC
& is the handle of operator.. i might in basic terms positioned that for the period of declarations for C++. For C you employ it in basic terms once you're sending a pointer the handle of a variable as in scanf("%s", &line) (which is going to truncate on the 1st whitespace char, use fgets(stdin...). Make count kind a pointer in case you may desire to bypass by reference.
Robert
2011-04-07 17:30:07 UTC
If you want to edit a variable with a function without returning it you have to pass it by reference, or pass a pointer to the variable.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...