Invalid array assignment while shifting struct elementfscanf() filterHow to sort Struct using qsort in C programmingreading, populating and writing a struct array to a binary fileget int value from array of char in c languageGetting the values of a char array in a loop pointed by another pointer to the arrayweek day finder strange behaviorHow to separate a date in c programming c date functionsHow do I combine two strings and then convert it to an integer in CHow to sort linked list by date?C Program Structs Assignment Invalid Octal Digit
How does this piece of code determine array size without using sizeof( )?
Combining two Lorentz boosts
Divisor Rich and Poor Numbers
Was Tyrion always a poor strategist?
Why does the U.S military use mercenaries?
What's is the easiest way to purchase a stock and hold it
French equivalent of the German expression "flöten gehen"
Why does a table with a defined constant in its index compute 10X slower?
Why does string strummed with finger sound different from the one strummed with pick?
Would a "ring language" be possible?
multicol package causes underfull hbox
What technology would Dwarves need to forge titanium?
Why is choosing a suitable thermodynamic potential important?
Will this series of events work to drown the Tarrasque?
Is it a good idea to teach algorithm courses using pseudocode?
Have GoT's showrunners reacted to the poor reception of the final season?
Hotel booking: Why is Agoda much cheaper than booking.com?
Is my company merging branches wrong?
How to draw pentagram-like shape in Latex?
Driving a school bus in the USA
Can the word crowd refer to just 10 people?
Gambler's Fallacy Dice
FIFO data structure in pure C
Pedaling at different gear ratios on flat terrain: what's the point?
Invalid array assignment while shifting struct element
fscanf() filterHow to sort Struct using qsort in C programmingreading, populating and writing a struct array to a binary fileget int value from array of char in c languageGetting the values of a char array in a loop pointed by another pointer to the arrayweek day finder strange behaviorHow to separate a date in c programming c date functionsHow do I combine two strings and then convert it to an integer in CHow to sort linked list by date?C Program Structs Assignment Invalid Octal Digit
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have to "delete" some elements from struct. For example let it be years.
So i should find them in my struct, shift them and display new struct without them.
Our professor said, that we should use pointers to structs. And i don't get it. I mean from my point of view structs works as arrays but with some specifications.
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
I guess it should shift data to "deleted" space but I get invalid array assignment with all char stuff.
Here are my struct
s
struct date
int day;
int month;
int year;
;
date datebase[100], *dtbs=datebase;
struct movies
int udk;
char name[10];
char genre[10];
char creator[10];
int releaseYear;
struct date movieRental;
;
movies mooviebase[100], *mvbs=mooviebase;
c
|
show 5 more comments
I have to "delete" some elements from struct. For example let it be years.
So i should find them in my struct, shift them and display new struct without them.
Our professor said, that we should use pointers to structs. And i don't get it. I mean from my point of view structs works as arrays but with some specifications.
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
I guess it should shift data to "deleted" space but I get invalid array assignment with all char stuff.
Here are my struct
s
struct date
int day;
int month;
int year;
;
date datebase[100], *dtbs=datebase;
struct movies
int udk;
char name[10];
char genre[10];
char creator[10];
int releaseYear;
struct date movieRental;
;
movies mooviebase[100], *mvbs=mooviebase;
c
could you please provide the definition of your struct ? what is mbvs ? what is pos ?
– Martin
Mar 23 at 17:27
My fault. Here: `````` struct date int day; int month; int year; ; date datebase[100], *dtbs=datebase; struct movies int udk; char name[10]; char genre[10]; char creator[10]; int releaseYear; struct date movieRental; ; movies mooviebase[100], *mvbs=mooviebase; ````
– umbrella123
Mar 23 at 17:28
You can edit your code for readability
– Martin
Mar 23 at 17:29
Please edit the code in your question rather than providing it in the comments. Please also make it a fully compilable program.
– brothir
Mar 23 at 17:34
The variable definitions are wrong, should bestruct date datebase[100], *dtbs=datebase;
andstruct movies mooviebase[100], *mvbs=mooviebase;
. The keywordstruct
is missing.
– Weather Vane
Mar 23 at 17:48
|
show 5 more comments
I have to "delete" some elements from struct. For example let it be years.
So i should find them in my struct, shift them and display new struct without them.
Our professor said, that we should use pointers to structs. And i don't get it. I mean from my point of view structs works as arrays but with some specifications.
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
I guess it should shift data to "deleted" space but I get invalid array assignment with all char stuff.
Here are my struct
s
struct date
int day;
int month;
int year;
;
date datebase[100], *dtbs=datebase;
struct movies
int udk;
char name[10];
char genre[10];
char creator[10];
int releaseYear;
struct date movieRental;
;
movies mooviebase[100], *mvbs=mooviebase;
c
I have to "delete" some elements from struct. For example let it be years.
So i should find them in my struct, shift them and display new struct without them.
Our professor said, that we should use pointers to structs. And i don't get it. I mean from my point of view structs works as arrays but with some specifications.
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
I guess it should shift data to "deleted" space but I get invalid array assignment with all char stuff.
Here are my struct
s
struct date
int day;
int month;
int year;
;
date datebase[100], *dtbs=datebase;
struct movies
int udk;
char name[10];
char genre[10];
char creator[10];
int releaseYear;
struct date movieRental;
;
movies mooviebase[100], *mvbs=mooviebase;
c
c
edited Mar 23 at 17:47
Weather Vane
27.6k52139
27.6k52139
asked Mar 23 at 17:22
umbrella123umbrella123
11
11
could you please provide the definition of your struct ? what is mbvs ? what is pos ?
– Martin
Mar 23 at 17:27
My fault. Here: `````` struct date int day; int month; int year; ; date datebase[100], *dtbs=datebase; struct movies int udk; char name[10]; char genre[10]; char creator[10]; int releaseYear; struct date movieRental; ; movies mooviebase[100], *mvbs=mooviebase; ````
– umbrella123
Mar 23 at 17:28
You can edit your code for readability
– Martin
Mar 23 at 17:29
Please edit the code in your question rather than providing it in the comments. Please also make it a fully compilable program.
– brothir
Mar 23 at 17:34
The variable definitions are wrong, should bestruct date datebase[100], *dtbs=datebase;
andstruct movies mooviebase[100], *mvbs=mooviebase;
. The keywordstruct
is missing.
– Weather Vane
Mar 23 at 17:48
|
show 5 more comments
could you please provide the definition of your struct ? what is mbvs ? what is pos ?
– Martin
Mar 23 at 17:27
My fault. Here: `````` struct date int day; int month; int year; ; date datebase[100], *dtbs=datebase; struct movies int udk; char name[10]; char genre[10]; char creator[10]; int releaseYear; struct date movieRental; ; movies mooviebase[100], *mvbs=mooviebase; ````
– umbrella123
Mar 23 at 17:28
You can edit your code for readability
– Martin
Mar 23 at 17:29
Please edit the code in your question rather than providing it in the comments. Please also make it a fully compilable program.
– brothir
Mar 23 at 17:34
The variable definitions are wrong, should bestruct date datebase[100], *dtbs=datebase;
andstruct movies mooviebase[100], *mvbs=mooviebase;
. The keywordstruct
is missing.
– Weather Vane
Mar 23 at 17:48
could you please provide the definition of your struct ? what is mbvs ? what is pos ?
– Martin
Mar 23 at 17:27
could you please provide the definition of your struct ? what is mbvs ? what is pos ?
– Martin
Mar 23 at 17:27
My fault. Here: `````` struct date int day; int month; int year; ; date datebase[100], *dtbs=datebase; struct movies int udk; char name[10]; char genre[10]; char creator[10]; int releaseYear; struct date movieRental; ; movies mooviebase[100], *mvbs=mooviebase; ````
– umbrella123
Mar 23 at 17:28
My fault. Here: `````` struct date int day; int month; int year; ; date datebase[100], *dtbs=datebase; struct movies int udk; char name[10]; char genre[10]; char creator[10]; int releaseYear; struct date movieRental; ; movies mooviebase[100], *mvbs=mooviebase; ````
– umbrella123
Mar 23 at 17:28
You can edit your code for readability
– Martin
Mar 23 at 17:29
You can edit your code for readability
– Martin
Mar 23 at 17:29
Please edit the code in your question rather than providing it in the comments. Please also make it a fully compilable program.
– brothir
Mar 23 at 17:34
Please edit the code in your question rather than providing it in the comments. Please also make it a fully compilable program.
– brothir
Mar 23 at 17:34
The variable definitions are wrong, should be
struct date datebase[100], *dtbs=datebase;
and struct movies mooviebase[100], *mvbs=mooviebase;
. The keyword struct
is missing.– Weather Vane
Mar 23 at 17:48
The variable definitions are wrong, should be
struct date datebase[100], *dtbs=datebase;
and struct movies mooviebase[100], *mvbs=mooviebase;
. The keyword struct
is missing.– Weather Vane
Mar 23 at 17:48
|
show 5 more comments
1 Answer
1
active
oldest
votes
Invalid array assignment while shifting struct element
Warning if I indent correctly your code it is :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
but this is not what you want because only udk is moved in the loop and for all the other element k value n-1 (I suppose pos <= n - 1
) so you do for instance (mvbs+n-1) -> name = (mvbs+n) -> name
, and n being visible the size you go out of the initialized part of the array or may be out of the array.
Of course you missed to add to do :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
However first what pos is ? and because you reuse/modify the variable i in the embedded for you cannot manage the case where several entries have the same releaseYear
Probably the first for is supposed to be on pos rather than on i to be more consistent and to remove the entries pos having the search year.
After the embedded for there is one less element so you need to decrease n
Note if there are several consecutive entries having the search year you can optimize to bypass them at the same time rather than to do one by one.
The type of the elements are compatible to a direct copy so the code can be simplified to be :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos) -> releaseYear)
for (i = pos; i < n - 1; i++)
*(mvbs+i ) = *(mvbs+i+1)
n -= 1;
else
pos += 1;
and finally you can use a memmove rather than to do the copy by yourself element by element :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos)->releaseYear)
if (pos == --n)
/* it was the last entry */
break;
memmove(mvbs+pos, mvbs+pos+1, (n-pos)*sizeof(movies));
else
pos += 1;
Our professor said, that we should use pointers to structs
You already use a pointer because you do not do mooviebase[pos]
for instance but use mvbs+offset, but that pointer never change and you need the additional offset, so your pointer is not useful, you can do for instance :
movies * ptr = mooviebase;
movies * sup = mooviebase + n;
while (ptr != sup)
if (yr == ptr->releaseYear)
if (ptr == --sup)
/* it was the last entry */
break;
memmove(ptr, ptr+1, (sup - ptr)*sizeof(movies));
else
ptr += 1;
and you too:mvbs[i] = mvbs[i + 1]
:D
– Antti Haapala
Mar 23 at 18:08
@AnttiHaapala 1) that changes nothing, 2) I follow the way to write of the OP to help him to see the errors comparing his code and mine, 3) the goal for the OP is to move to the use of pointer , so ... ;-)
– bruno
Mar 23 at 18:14
Mind you that[]
operator is only defined for pointers, about time we teach the people that.
– Antti Haapala
Mar 23 at 18:20
@AnttiHaapala c'mon, using pointer means to do*mvbs
and++mvbs
for instance, do you really suppose I am stupid ? I hope not ^^ Did you read the end of my answer ?
– bruno
Mar 23 at 18:22
I am not sure if you're sarcastic or not, let me reiterate -foo[i]
operator requires a pointer as one of the operands, so using*(foo + bar)
isn't "more using pointers", it just looks awkward.
– Antti Haapala
Mar 23 at 18:52
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55316426%2finvalid-array-assignment-while-shifting-struct-element%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Invalid array assignment while shifting struct element
Warning if I indent correctly your code it is :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
but this is not what you want because only udk is moved in the loop and for all the other element k value n-1 (I suppose pos <= n - 1
) so you do for instance (mvbs+n-1) -> name = (mvbs+n) -> name
, and n being visible the size you go out of the initialized part of the array or may be out of the array.
Of course you missed to add to do :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
However first what pos is ? and because you reuse/modify the variable i in the embedded for you cannot manage the case where several entries have the same releaseYear
Probably the first for is supposed to be on pos rather than on i to be more consistent and to remove the entries pos having the search year.
After the embedded for there is one less element so you need to decrease n
Note if there are several consecutive entries having the search year you can optimize to bypass them at the same time rather than to do one by one.
The type of the elements are compatible to a direct copy so the code can be simplified to be :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos) -> releaseYear)
for (i = pos; i < n - 1; i++)
*(mvbs+i ) = *(mvbs+i+1)
n -= 1;
else
pos += 1;
and finally you can use a memmove rather than to do the copy by yourself element by element :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos)->releaseYear)
if (pos == --n)
/* it was the last entry */
break;
memmove(mvbs+pos, mvbs+pos+1, (n-pos)*sizeof(movies));
else
pos += 1;
Our professor said, that we should use pointers to structs
You already use a pointer because you do not do mooviebase[pos]
for instance but use mvbs+offset, but that pointer never change and you need the additional offset, so your pointer is not useful, you can do for instance :
movies * ptr = mooviebase;
movies * sup = mooviebase + n;
while (ptr != sup)
if (yr == ptr->releaseYear)
if (ptr == --sup)
/* it was the last entry */
break;
memmove(ptr, ptr+1, (sup - ptr)*sizeof(movies));
else
ptr += 1;
and you too:mvbs[i] = mvbs[i + 1]
:D
– Antti Haapala
Mar 23 at 18:08
@AnttiHaapala 1) that changes nothing, 2) I follow the way to write of the OP to help him to see the errors comparing his code and mine, 3) the goal for the OP is to move to the use of pointer , so ... ;-)
– bruno
Mar 23 at 18:14
Mind you that[]
operator is only defined for pointers, about time we teach the people that.
– Antti Haapala
Mar 23 at 18:20
@AnttiHaapala c'mon, using pointer means to do*mvbs
and++mvbs
for instance, do you really suppose I am stupid ? I hope not ^^ Did you read the end of my answer ?
– bruno
Mar 23 at 18:22
I am not sure if you're sarcastic or not, let me reiterate -foo[i]
operator requires a pointer as one of the operands, so using*(foo + bar)
isn't "more using pointers", it just looks awkward.
– Antti Haapala
Mar 23 at 18:52
|
show 1 more comment
Invalid array assignment while shifting struct element
Warning if I indent correctly your code it is :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
but this is not what you want because only udk is moved in the loop and for all the other element k value n-1 (I suppose pos <= n - 1
) so you do for instance (mvbs+n-1) -> name = (mvbs+n) -> name
, and n being visible the size you go out of the initialized part of the array or may be out of the array.
Of course you missed to add to do :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
However first what pos is ? and because you reuse/modify the variable i in the embedded for you cannot manage the case where several entries have the same releaseYear
Probably the first for is supposed to be on pos rather than on i to be more consistent and to remove the entries pos having the search year.
After the embedded for there is one less element so you need to decrease n
Note if there are several consecutive entries having the search year you can optimize to bypass them at the same time rather than to do one by one.
The type of the elements are compatible to a direct copy so the code can be simplified to be :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos) -> releaseYear)
for (i = pos; i < n - 1; i++)
*(mvbs+i ) = *(mvbs+i+1)
n -= 1;
else
pos += 1;
and finally you can use a memmove rather than to do the copy by yourself element by element :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos)->releaseYear)
if (pos == --n)
/* it was the last entry */
break;
memmove(mvbs+pos, mvbs+pos+1, (n-pos)*sizeof(movies));
else
pos += 1;
Our professor said, that we should use pointers to structs
You already use a pointer because you do not do mooviebase[pos]
for instance but use mvbs+offset, but that pointer never change and you need the additional offset, so your pointer is not useful, you can do for instance :
movies * ptr = mooviebase;
movies * sup = mooviebase + n;
while (ptr != sup)
if (yr == ptr->releaseYear)
if (ptr == --sup)
/* it was the last entry */
break;
memmove(ptr, ptr+1, (sup - ptr)*sizeof(movies));
else
ptr += 1;
and you too:mvbs[i] = mvbs[i + 1]
:D
– Antti Haapala
Mar 23 at 18:08
@AnttiHaapala 1) that changes nothing, 2) I follow the way to write of the OP to help him to see the errors comparing his code and mine, 3) the goal for the OP is to move to the use of pointer , so ... ;-)
– bruno
Mar 23 at 18:14
Mind you that[]
operator is only defined for pointers, about time we teach the people that.
– Antti Haapala
Mar 23 at 18:20
@AnttiHaapala c'mon, using pointer means to do*mvbs
and++mvbs
for instance, do you really suppose I am stupid ? I hope not ^^ Did you read the end of my answer ?
– bruno
Mar 23 at 18:22
I am not sure if you're sarcastic or not, let me reiterate -foo[i]
operator requires a pointer as one of the operands, so using*(foo + bar)
isn't "more using pointers", it just looks awkward.
– Antti Haapala
Mar 23 at 18:52
|
show 1 more comment
Invalid array assignment while shifting struct element
Warning if I indent correctly your code it is :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
but this is not what you want because only udk is moved in the loop and for all the other element k value n-1 (I suppose pos <= n - 1
) so you do for instance (mvbs+n-1) -> name = (mvbs+n) -> name
, and n being visible the size you go out of the initialized part of the array or may be out of the array.
Of course you missed to add to do :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
However first what pos is ? and because you reuse/modify the variable i in the embedded for you cannot manage the case where several entries have the same releaseYear
Probably the first for is supposed to be on pos rather than on i to be more consistent and to remove the entries pos having the search year.
After the embedded for there is one less element so you need to decrease n
Note if there are several consecutive entries having the search year you can optimize to bypass them at the same time rather than to do one by one.
The type of the elements are compatible to a direct copy so the code can be simplified to be :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos) -> releaseYear)
for (i = pos; i < n - 1; i++)
*(mvbs+i ) = *(mvbs+i+1)
n -= 1;
else
pos += 1;
and finally you can use a memmove rather than to do the copy by yourself element by element :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos)->releaseYear)
if (pos == --n)
/* it was the last entry */
break;
memmove(mvbs+pos, mvbs+pos+1, (n-pos)*sizeof(movies));
else
pos += 1;
Our professor said, that we should use pointers to structs
You already use a pointer because you do not do mooviebase[pos]
for instance but use mvbs+offset, but that pointer never change and you need the additional offset, so your pointer is not useful, you can do for instance :
movies * ptr = mooviebase;
movies * sup = mooviebase + n;
while (ptr != sup)
if (yr == ptr->releaseYear)
if (ptr == --sup)
/* it was the last entry */
break;
memmove(ptr, ptr+1, (sup - ptr)*sizeof(movies));
else
ptr += 1;
Invalid array assignment while shifting struct element
Warning if I indent correctly your code it is :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
but this is not what you want because only udk is moved in the loop and for all the other element k value n-1 (I suppose pos <= n - 1
) so you do for instance (mvbs+n-1) -> name = (mvbs+n) -> name
, and n being visible the size you go out of the initialized part of the array or may be out of the array.
Of course you missed to add to do :
for(i = 0; i < n; ++i)
if ( yr == (mvbs+i) -> releaseYear)
for (i = pos; i < n - 1; i++)
(mvbs+i ) -> udk = (mvbs+i+1) -> udk; //int
(mvbs+i) -> name = (mvbs+i+1) -> name; //char
(mvbs+i) -> genre = (mvbs+i+1) -> genre;//char
(mvbs+i) -> creator = (mvbs+i+1) -> creator;//char
(mvbs+i) -> releaseYear = (mvbs+i+1) -> releaseYear;//int
(dtbs+i) -> day = (dtbs+i+1) -> day;//int
(dtbs+i) -> month = (dtbs+i+1) -> month;//int
(dtbs+i) -> year = (dtbs+i+1) -> year;//int
However first what pos is ? and because you reuse/modify the variable i in the embedded for you cannot manage the case where several entries have the same releaseYear
Probably the first for is supposed to be on pos rather than on i to be more consistent and to remove the entries pos having the search year.
After the embedded for there is one less element so you need to decrease n
Note if there are several consecutive entries having the search year you can optimize to bypass them at the same time rather than to do one by one.
The type of the elements are compatible to a direct copy so the code can be simplified to be :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos) -> releaseYear)
for (i = pos; i < n - 1; i++)
*(mvbs+i ) = *(mvbs+i+1)
n -= 1;
else
pos += 1;
and finally you can use a memmove rather than to do the copy by yourself element by element :
pos = 0;
while(pos < n)
if ( yr == (mvbs+pos)->releaseYear)
if (pos == --n)
/* it was the last entry */
break;
memmove(mvbs+pos, mvbs+pos+1, (n-pos)*sizeof(movies));
else
pos += 1;
Our professor said, that we should use pointers to structs
You already use a pointer because you do not do mooviebase[pos]
for instance but use mvbs+offset, but that pointer never change and you need the additional offset, so your pointer is not useful, you can do for instance :
movies * ptr = mooviebase;
movies * sup = mooviebase + n;
while (ptr != sup)
if (yr == ptr->releaseYear)
if (ptr == --sup)
/* it was the last entry */
break;
memmove(ptr, ptr+1, (sup - ptr)*sizeof(movies));
else
ptr += 1;
edited Mar 23 at 19:57
answered Mar 23 at 17:56
brunobruno
17.1k31528
17.1k31528
and you too:mvbs[i] = mvbs[i + 1]
:D
– Antti Haapala
Mar 23 at 18:08
@AnttiHaapala 1) that changes nothing, 2) I follow the way to write of the OP to help him to see the errors comparing his code and mine, 3) the goal for the OP is to move to the use of pointer , so ... ;-)
– bruno
Mar 23 at 18:14
Mind you that[]
operator is only defined for pointers, about time we teach the people that.
– Antti Haapala
Mar 23 at 18:20
@AnttiHaapala c'mon, using pointer means to do*mvbs
and++mvbs
for instance, do you really suppose I am stupid ? I hope not ^^ Did you read the end of my answer ?
– bruno
Mar 23 at 18:22
I am not sure if you're sarcastic or not, let me reiterate -foo[i]
operator requires a pointer as one of the operands, so using*(foo + bar)
isn't "more using pointers", it just looks awkward.
– Antti Haapala
Mar 23 at 18:52
|
show 1 more comment
and you too:mvbs[i] = mvbs[i + 1]
:D
– Antti Haapala
Mar 23 at 18:08
@AnttiHaapala 1) that changes nothing, 2) I follow the way to write of the OP to help him to see the errors comparing his code and mine, 3) the goal for the OP is to move to the use of pointer , so ... ;-)
– bruno
Mar 23 at 18:14
Mind you that[]
operator is only defined for pointers, about time we teach the people that.
– Antti Haapala
Mar 23 at 18:20
@AnttiHaapala c'mon, using pointer means to do*mvbs
and++mvbs
for instance, do you really suppose I am stupid ? I hope not ^^ Did you read the end of my answer ?
– bruno
Mar 23 at 18:22
I am not sure if you're sarcastic or not, let me reiterate -foo[i]
operator requires a pointer as one of the operands, so using*(foo + bar)
isn't "more using pointers", it just looks awkward.
– Antti Haapala
Mar 23 at 18:52
and you too:
mvbs[i] = mvbs[i + 1]
:D– Antti Haapala
Mar 23 at 18:08
and you too:
mvbs[i] = mvbs[i + 1]
:D– Antti Haapala
Mar 23 at 18:08
@AnttiHaapala 1) that changes nothing, 2) I follow the way to write of the OP to help him to see the errors comparing his code and mine, 3) the goal for the OP is to move to the use of pointer , so ... ;-)
– bruno
Mar 23 at 18:14
@AnttiHaapala 1) that changes nothing, 2) I follow the way to write of the OP to help him to see the errors comparing his code and mine, 3) the goal for the OP is to move to the use of pointer , so ... ;-)
– bruno
Mar 23 at 18:14
Mind you that
[]
operator is only defined for pointers, about time we teach the people that.– Antti Haapala
Mar 23 at 18:20
Mind you that
[]
operator is only defined for pointers, about time we teach the people that.– Antti Haapala
Mar 23 at 18:20
@AnttiHaapala c'mon, using pointer means to do
*mvbs
and ++mvbs
for instance, do you really suppose I am stupid ? I hope not ^^ Did you read the end of my answer ?– bruno
Mar 23 at 18:22
@AnttiHaapala c'mon, using pointer means to do
*mvbs
and ++mvbs
for instance, do you really suppose I am stupid ? I hope not ^^ Did you read the end of my answer ?– bruno
Mar 23 at 18:22
I am not sure if you're sarcastic or not, let me reiterate -
foo[i]
operator requires a pointer as one of the operands, so using *(foo + bar)
isn't "more using pointers", it just looks awkward.– Antti Haapala
Mar 23 at 18:52
I am not sure if you're sarcastic or not, let me reiterate -
foo[i]
operator requires a pointer as one of the operands, so using *(foo + bar)
isn't "more using pointers", it just looks awkward.– Antti Haapala
Mar 23 at 18:52
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55316426%2finvalid-array-assignment-while-shifting-struct-element%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
could you please provide the definition of your struct ? what is mbvs ? what is pos ?
– Martin
Mar 23 at 17:27
My fault. Here: `````` struct date int day; int month; int year; ; date datebase[100], *dtbs=datebase; struct movies int udk; char name[10]; char genre[10]; char creator[10]; int releaseYear; struct date movieRental; ; movies mooviebase[100], *mvbs=mooviebase; ````
– umbrella123
Mar 23 at 17:28
You can edit your code for readability
– Martin
Mar 23 at 17:29
Please edit the code in your question rather than providing it in the comments. Please also make it a fully compilable program.
– brothir
Mar 23 at 17:34
The variable definitions are wrong, should be
struct date datebase[100], *dtbs=datebase;
andstruct movies mooviebase[100], *mvbs=mooviebase;
. The keywordstruct
is missing.– Weather Vane
Mar 23 at 17:48