= 4 and <= 8#include<stdio.h>int main(void) { char rec[9]; //you may need to adjust the ..." />

      asked    Merlin     2018-10-22       c       72 view        4 Answers

[SOLVED] How to fix "error:assignment to expression with array type"?

I'm getting an error:

error:assignment to expression with array type

in this program, which should print words that have 4-8 letters:

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

int main()
{
   char rec[300];
   int zbroj1=0, br=0, zbroj2=0;

   while((rec=getchar()) != '\n')
   {
     zbroj1++;
   }
   if(zbroj1>=4 || zbroj1<=8)
    printf("Nova recenica je: %s", zbroj1);

   return 0;
}

How can I fix this?

  4 Answers  

        answered    Bernie     2018-10-22      

Below is the code to print words of size >= 4 and <= 8

#include<stdio.h>

int main(void) {
    char rec[9];   //you may need to adjust the buffer according to the size of the words
    int br=0, zbroj2=0;

    while((zbroj2=getchar()) != '\n') {
        if(br < 8)
            rec[br] = zbroj2;    // we are not going to store the words having length more than 8
        br++;
        if(zbroj2 == 32){
            if(br>4 && br<=8){
                rec[br] = '\0';
                printf("Nova recenica je: %s\n", rec);
            }
            br = 0;
        }
    }
    if(br>=4 && br<=8){    // This check is to catch the last word
        rec[br] = '\0';
        printf("Nova recenica je: %s\n", rec);
    }
    return 0;
}


        answered    Bridget     2018-10-22      

There are two issues. First one is with return value of getchar(). This

rec=getchar()

surely causes warning as getchar() returns type is of int not of type of rec. From the manual page of getchar()

int getchar(void);

RETURN VALUE

getchar() return the character read as an unsigned char cast to an int or EOF on end of file or error.

And secondaly use logical-And && instaed of logical-OR || as && prints rec if zbroj1 is between 4 and 8 i.e when both condition are true , for e.g

if(zbroj1>=4 && zbroj1<=8)

Working code

#include<stdio.h>
int main(void) {
   int ret; 
   char rec[300];
   int zbroj1=0, br=0, zbroj2=0;

   while((ret=getchar()) != '\n') {
     rec[zbroj1] = ret; /* you need to store into array */
     zbroj1++;
   }
   rec[zbroj1] = '\0'; /* terminate the array with \0 */

   if(zbroj1>=4 && zbroj1<=8) /* use logical && */
    printf("Nova recenica je: %s", rec); /* use %d as zbroj1 is of int type*/
   return 0;
}


        answered    Phoenix     2018-10-22      

This is where the error is coming from...

rec=getchar()

...as rec is an array of char and getchar() returns a character as an int. Using the unused variable br to store that return value, and the counter variable zbroj1 as an index into the array you can build up rec using the following code:

while((br=getchar()) != '\n')
{
    rec[zbroj1]=br;
    zbroj1++;
}

Strings in C need to have a \0 character at the end to terminate them so you'll also need to have the line to finish it

rec[zbroj1]='\0';

And then your code to check the length has a logical error - it's using || which means either expression has to be true and you're printing out a string, so you want to use && (and) to ensure that zbroj1 is inbetween 4 & 8 inclusively.

if(zbroj1>=4 && zbroj1<=8)

Finally, you're outputting a string, but passing an int. To print out the word, you need to pass in the string you've built in rec

 printf("Nova recenica je: %s", rec);

Putting all this corrections together gets you this final version of your code:

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

int main()
{
   char rec[300];
   int zbroj1=0, br=0, zbroj2=0;

   while((br=getchar()) != '\n')
   {
     rec[zbroj1]=br;
     zbroj1++;
   }
   rec[zbroj1]='\0';
   if(zbroj1>=4 && zbroj1<=8)
    printf("Nova recenica je: %s", re);


   return 0;
}


        answered    Joanne     2018-10-22      

There are a few issues in your code :

  • you're assigning to an array :

    while((rec=getchar()) != '\n')

    getchar returns a int, so rec should be defined as such :

    int rec;

  • you're printing an int as if it were a string :

    printf("Nova recenica je: %s", zbroj1);

    This is undefined behavior. Use the correct format specifier :

    printf("Nova recenica je: %d", zbroj1);

  • your if check does not do what you expect :

    if(zbroj1>=4 || zbroj1<=8)

    This covers all values. You probably meant :

    if(zbroj1>=4 && zbroj1<=8)





Your Answer





 2018-10-22         Earl

azure active directory + spring boot

I am trying to run azure-active-directory-spring-boot-backend-sample but it is not working. After opening localhost:8080 it prompts with localhost:8080/login page and Azure link. After clicking on it it goes to Azure Active Directory page. After successful login it comes back to /login page rather than /welcome page. I tried using other example as well available on internet but nothing seems to be working. Please advise. You need to register your application under the Azure Active Directory where your users are stored. Then enter the published homepage URL in the Reply U...
 spring-boot                     1 answers                     50 view
 2018-10-22         Vanessa

Developing UCWA applications for Skype for Business Online

I'm trying to develop a simple java client that needs to access Skype for Business APIs in order to setup an online meeting (i.e. I need to retrieve a simple setup meeting URL by invoking an UCWA API).I followed this Microsoft tutorial step-by-step:https://docs.microsoft.com/en-us/skype-sdk/ucwa/developingucwaapplicationsforsfbonlineI have an Office 365 Business Premium license and I configured a custom domain (correctly registered and added at the zone DNS file);I configured and registered my java client app on Azure (taking care to assign required delegated permission for...
 java                     1 answers                     51 view
 2018-10-22         Fabian

npm linked module listed as unmet dependency

I'm linking my-lib to see effects in my-app right away. cd my-lib-foldernpm linkcd ../my-app-foldernpm link my-libThis works as expected. Changes are picked up by my-app and I see the symlink in node_modules. My problem is that in my-app-folder/, npm list --link=true shows up empty. How can I get a list of the linked modules? npm ls my_libmy-app@3.1.0 /Users/path/my_project└── UNMET DEPENDENCY my-lib@3.7.0 npm ERR! missing: my-lib@3.7.0, required by my-app@3.1.0This happens both with Node 8 and 10 and their respective npm versions. OK, I realized that my-lib was somehow ...
 node.js                     1 answers                     50 view