Jump to content

Can someone point the mistake... C

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
34 replies to this topic

#1
digerati

digerati

    Newbie

  • Members
  • Pip
  • 9 posts
Hi coders,

I was trying to write the source for ls, and i was able to come this far,

#include<stdio.h>
#include<string.h>
#include<dirent.h>
#include<sys/types.h>

int ls_on_dir(char *);
int print_aggr_fail_list(char **);

int main(int argc, char ** argv)
{
	int exit_status; char **fail_list = NULL;
	if (argc-- == 1) return (ls_on_dir("."));
	else do {
		exit_status = ls_on_dir(++argv);
		if (exit_status) fail_list++ = argv;
	}while (--argc > 0);
	print_aggr_fail_list(fail_list);
	return (0);
}

int ls_on_dir(char *direc) {
	DIR *dir_ptr; struct dirent *direcp_ptr;
	if ((dir_ptr = opendir(direc)) == NULL) return (1);
	else {
		while (direcp_ptr = readdir(dir_ptr))
			printf("%s\t", direcp_ptr->d_name);
		closedir(dir_ptr);
	}
	return (0);
}
But i get an invalid lvalue assignment at line 24. Can't seem to figure it out.

Thank you.

Edited by v0id, 27 September 2008 - 04:17 AM.
Remember code-tags!


#2
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
while (direcp_ptr = readdir(dir_ptr))

should it be

while (direcp_ptr == readdir(dir_ptr))?

Is that line 24...? :D
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#3
digerati

digerati

    Newbie

  • Members
  • Pip
  • 9 posts
if (exit_status) fail_list++ = argv;

The error is in this line, it says invalid lvalue... don't know why?

#4
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
could you write
if (exit_status)
{
fail_list++;
fail_list = argv;
}
Is that what you're trying to do?
i++ = something is not legal I think...

Edited by marwex89, 28 September 2008 - 04:06 AM.
Forgot a ";" :D

Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
marwex89 is right. Because ++ is a binary operator, you can't have an assignment, because that's a second operand, in a sense. And why would you want to increment something and then reassign it? You might as well just leave out the ++ part.

#6
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
Yup :p
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#7
digerati

digerati

    Newbie

  • Members
  • Pip
  • 9 posts
Thank you for your quick replies (marwex89 and dargueta) marwex89) you guys are right and it solved the problem, Still i am confused a little bit on why the fail_list++ = argv did not work, cause if the statement was something like fail_list++ = 5, i think it would assign the value 5 to fail_list and then increment fail_list, lets say fail_list is just int in this case. Correct me if i am wrong.

#8
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
I get your logic, but I don't think C does :D
But ++fail_list = 5 will work, I think...?
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#9
digerati

digerati

    Newbie

  • Members
  • Pip
  • 9 posts
Yeah... i think so too, thanks anyways for your help.

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
Just do fail_list = n + 1. Much simpler and easier to understand.

#11
marwex89

marwex89

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 10,720 posts
You mean he could write "fail_list = 5 + 1"?
That would not be the same...? :?
Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,709 posts
assume k,n in R


s = k + 1
s = s + n

is the same as

s = k
s = n + 1