Thread: Lvalue error? Whats that?

  1. #16
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Hey guys thanks very much for your input. My prof does brackets like

    Code:
    if (x) {
    ...
    }
    but I see a lot of code on these forums look like the following so I adapt them that way (plus its easier to make sure your syntax is correct)
    Code:
    if (x)
    {
    ...
    }
    BUT, I have one more issue guys which I would really love your help on. After resubstituting the corrections in my program doesn't display the sorted words...at all...the program comes to a halt with this code ;

    (really sorry for the eye sore, don't have time to tidy it up)

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char **p;
    char size[20];
    char buff[21];
    char temp[21];
    int N;
    int j,i,k,l,m,d,s,a,b;
    
    int main()
    {
    	do{
    		d=0;
    		printf("How many words would you like: ");
    		fflush(stdout);
    		for(j=0; j<20; j++)
    		 {
    		      size[j]=fgetc(stdin);
    		      if (size[j]=='\n')
    		      {
    		        size[j]='\0';
    		        break;
    		      }
    		 }/* end j-loop */
    			if (j==0){
    				printf("That input is empty.\n");
    				d=1;
    
    			}
    		    if (j==20) {
    		      printf("That input is too long.\n");
    		      while(fgetc(stdin)!='\n');
    		      d=1;
    		  }
    }while(d!=0);
    
    
    	printf("You want %d words.\n", atoi(size));
    
    	N=atoi(size);
    	p=(char**)malloc(N*sizeof(char*));
    		if (p==NULL)
    		{
    			printf("Memory error");
    			exit(0);
    		}
    	for (i=0; i<N; i++)
    		p[i]=(char*)malloc(21*sizeof(char));
    		if (p[i]==NULL)
    		{
    			printf("Memory error");
    			exit(0);
    		}
    	printf("Do not enter more than 20 characters.\n");
    
    	for(k=0; k<N; k++)
    	{
    		A:printf("Enter a word: ");
    		fflush(stdout);
    		for(m=0; m<21; m++)
    		{
    		      buff[m]=(char)fgetc(stdin);
    		      if (buff[m]=='\n')
    		      {
    		        buff[m]='\0';
    		        break;
    		      }
    		 }
    
    		    if (m>=21)
    		    {
    		    	printf("Word is more than 20 characters.\n");
    		      	while(fgetc(stdin)!='\n');
          			goto A;
    			}
    			if (m==0)
    			{
    				printf("That input is empty\n");
    				goto A;
    			}
    		strcpy(p[k],buff);
    	}
    	printf("\n");
    	printf("Your unsorted list of Names:\n");
    	k=0;
    	for(k=0; k<N; k++)
    		printf("%s\n",p[k]);
    
    
    	for (a=N; a>0; a--)
    	{
    		for (b=0; b<a; b++)
    		{
    			if(strcmp(p[b],p[b+1])>0)
    			{
    				strcpy(temp,p[b]);
                                    strcpy(p[b],p[b+1]);
                                    strcpy(p[b+1], temp);
    			}
    		}
    
    	}
    	printf("\n");
    	printf("Your sorted list of Names:\n");
    	k=0;
    	for(k=0; k<N; k++)
    		printf("%s\n",p[k]);
    
    
    
    return 0;
    }
    The program just...stops at "Your sorted output is :" and you have to CTRL + break to get out of it.

    When N (the number of words they want) is 0 -zero- the "sorted" output is a smiley face...lol does that point out what I am doing wrong?

    The code provided by my professor was;
    Code:
    		        if(strcmp(p[b],p[b+1])>0)
    			{
    				temp=p[b];
                                    p[b]=p[b+1];
                                    p[b+1]=temp;
    			}
    Basically he illustrated it as changing the location of the pointers (which makes sence) however using that code gives me Lvalue issues(which I don't know what that error means).

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    temp is not a pointer, however. If temp was a pointer, then your professor's code would work.
    As for the bracket issue, you are free to choose your own one. My preferred one is the latter example you showed.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    See

    http://en.wikipedia.org/wiki/Indent_style

    The first code snippet is an example of K&R, the second of Allman. Allman has the advantage of treating the opening and closing braces symmetrically, but K&R saves vertical screen space. I've toyed with the idea of using Pico style (described in the link) which both treats the braces symmetrically and saves screen space, but unfortunately almost no one else seems to use it.

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Quote Originally Posted by Elysia View Post
    temp is not a pointer, however. If temp was a pointer, then your professor's code would work.
    So i used
    Code:
    char *temp
    ...
    temp=(char*)malloc(21*sizeof(char));
    		if (p==NULL)
    		{
    			printf("Memory error");
    			exit(0);
    		}
    ...
    
    for (a=N; a>0; a--)
    	{
    		for (b=0; b<a; b++)
    		{
    			if(strcmp(p[b],p[b+1])>0)
    			{
    				temp=p[b];
                                    p[b]=p[b+1];
                                    p[b+1]=temp;
    			}
    		}
    
    	}
    And I still have the same issue of a halt in my program,

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by robatino View Post
    I've toyed with the idea of using Pico style (described in the link) which both treats the braces symmetrically and saves screen space, but unfortunately almost no one else seems to use it.
    Pico style is bad, horrible and poor. It absolutely destroys readability of code.
    And what for? Saving two lines of code? I do not see the point in that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >temp=(char*)malloc(21*sizeof(char));
    You don't need this line since you're just swapping pointers.

    >for (a=N; a>0; a--)
    This should probably be:
    for (a=N-1; a>0; a--)
    Otherwise you wind up running off the end of the array in the b loop (b+1).

  7. #22
    Registered User
    Join Date
    Feb 2008
    Posts
    35
    Quote Originally Posted by swoopy View Post
    >temp=(char*)malloc(21*sizeof(char));
    You don't need this line since you're just swapping pointers.

    >for (a=N; a>0; a--)
    This should probably be:
    for (a=N-1; a>0; a--)
    Otherwise you wind up running off the end of the array in the b loop (b+1).
    Yes! It works! thank you so much man! I'll have to just think through that correction though so I understand what it was doing

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A pointer is just a variable with a memory address stored. The pointer does not store the actual data.
    Instead of swapping actual CDs in an office, you swap the address of where to find them. That's basically what the pointers does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Elysia View Post
    Pico style is bad, horrible and poor. It absolutely destroys readability of code.
    And what for? Saving two lines of code? I do not see the point in that.
    Objectively, it's no less readable than K&R or Allman. In all three, to the human reader, scope is determined by a statement's horizontal offset. If the braces were invisible, the code could be read almost as easily. The three styles essentially just generate different amounts of vertical white space.

  10. #25
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    for(j=0; j<20; j++)
    		 {
    		      size[j]=fgetc(stdin);
    		      if (size[j]=='\n')
    		      {
    		        size[j]='\0';
    		        break;
    		      }
    		 }/
    use fgets - one call instead of the loop is more readable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by robatino View Post
    Objectively, it's no less readable than K&R or Allman. In all three, to the human reader, scope is determined by a statement's horizontal offset. If the braces were invisible, the code could be read almost as easily. The three styles essentially just generate different amounts of vertical white space.
    This is not true to many. The brackets help define the beginning and end of a block. Especially when there's no good indentation.
    And when using a style like that, you hide the bracket is obscure places, making them hard to find.
    And what's the deal with saving two lines of vertical space!? Saving horizontal space is far more important.

    In essence, it hurts readability.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Elysia View Post
    This is not true to many. The brackets help define the beginning and end of a block. Especially when there's no good indentation.
    And when using a style like that, you hide the bracket is obscure places, making them hard to find.
    And what's the deal with saving two lines of vertical space!? Saving horizontal space is far more important.

    In essence, it hurts readability.
    I don't think anyone who indents their code properly should care about whether the braces can be found easily in unindented code. And if the code is indented properly, in any style, each scope can be identified by its horizontal offset, so one doesn't even need to see the braces (only the compiler needs that, since it ignores the whitespace). I don't understand why you think saving horizontal space is more important - if lines are long enough that they start wrapping around, far more horizontal space can be recovered by reducing each scope's horizontal offset (by going from 4 spaces to 2, say) than by putting braces on separate lines. On the other hand, the vertical space saved by the more compact styles can be considerable, since it's one or two lines saved for each scope, and each scope may only be a few lines to begin with.

  13. #28
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I don't think that's a debate that's ever going to get anywhere. Use the style you prefer, but use it consistently.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by robatino View Post
    I don't think anyone who indents their code properly should care about whether the braces can be found easily in unindented code.
    I think they should because it makes it easier to identify a scope.

    And if the code is indented properly, in any style, each scope can be identified by its horizontal offset, so one doesn't even need to see the braces (only the compiler needs that, since it ignores the whitespace).
    Brackets makes it easier to identify scope. Sometimes you can make mistakes, too. Brackets helps solve that.

    I don't understand why you think saving horizontal space is more important - if lines are long enough that they start wrapping around, far more horizontal space can be recovered by reducing each scope's horizontal offset (by going from 4 spaces to 2, say) than by putting braces on separate lines. On the other hand, the vertical space saved by the more compact styles can be considerable, since it's one or two lines saved for each scope, and each scope may only be a few lines to begin with.
    What I don't understand is why it's so important to save vertical space.
    Horizontal is obvious - it's annoying to have to scroll to change the code.
    But with vertical, it's not much so. You do not save much space by compacting the braces at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lvalue rvalue discussion
    By George2 in forum C++ Programming
    Replies: 18
    Last Post: 03-04-2008, 05:48 AM
  2. A non-const reference may only be bound to an lvalue?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2007, 06:42 PM
  3. how to use reference for lvalue
    By jabka in forum C++ Programming
    Replies: 9
    Last Post: 04-22-2007, 02:08 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM