Re: invalid lvalue in assignment
- From: Chad <cdalten@xxxxxxxxx>
- Date: Fri, 9 Jan 2009 18:24:30 -0800 (PST)
On Jan 9, 6:00 pm, "Default User" <defaultuse...@xxxxxxxxx> wrote:
Chad wrote:
On Jan 9, 5:12 pm, "Default User" <defaultuse...@xxxxxxxxx> wrote:
1. You pass in a pointer to pointer, but no dimensions. I suppose
it's supposed to be an array of strings, based on main(). However,
there's no way of associating nrows and ncolumns with that. What
does 10x10 have to do with anything? Certainly not the array of two
strings of length four you passed in.
I know the nrow and ncolumns should be passed to choparray(). But
quite frankly, it sort of hurt when I thought about how to actually do
it.
void choparray(char **s, int nrows, int ncolumns);
Then use those just like you did. To call:
choparray(stuff, 2, 5); /* or whatever */
I still have doubts about this whole thing. We'll see what the next
paragraph brings. I'm atingle.
2. You allocate all that memory, then leak it at the end. You
should be returning it or something. If, based on the name of the
function, you're planning to lop a character off the end of
strings, then you don't need to allocate anything. Explain the goal
here.
I was too lazy to use free() and there is no goal here.
What a letdown.
What is the purpose of choparray()? Right now all it does (if it were
working) is print out one string minus its last character. You could do
that a whole lot easier:
size_t len = strlen(s[0]);
int i;
for (i = 0; i < len-1; i++)
{
putchar(s[0][i]);
}
Presumably that's not it, so what ARE you trying to achieve with the
function? It LOOKS like you want to remove the last character, that's
normally what a chop function does. Why are you making a copy of it? Do
you not want to change the original array?
Explain the purpose of the function. Presumably it has a purpose, even
if only an academic one. What are the requirements of the function? Why
do you pass in what you do? What should be the overall results of the
function after it runs? These are things you decide before you ever
write a line of code.
If you don't have a clear idea of what the function should do, how will
you ever know if it's doing that?
The whole little program stemed from the 'pointer Access violation
thread' on this forum found at the following URL...
http://groups.google.com/group/comp.lang.c/browse_thread/thread/596801078c7d57d9?hl=en#
Basically the original poster wanted to remove the last character in
each array element. He never specified if he wanted to modify the
array in place. Anhow, it sort of looked like an interesting excercise
at the time. Anyhow, here is what I came up with...
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void choparray(char **s, int nrows)
{
int i;
size_t j;
char **buf = malloc(nrows * sizeof(char *));
for(i = 0; i < nrows; i++) {
j = strlen(*s) -1;
buf[i] = malloc(j * sizeof(char));
strcpy(*(buf + i), *s);
buf[i][j] = '\0';
*(s++); /*s[something]*/
}
for (i = 0; i < nrows; i ++) {
printf("%s\n", *(buf + i));
}
for(i = 0; i < nrows; i++)
free((void *)buf[i]);
free((void *)buf);
}
int main(void)
{
int number;
char *stuff[]= {"test", "this", "input", "data"};
number = (sizeof stuff/sizeof stuff[0]);
choparray(stuff, number);
return 0;
}
[cdalten@localhost oakland]$ gcc -g -Wall mall.c -o mall
mall.c: In function ‘choparray’:
mall.c:16: warning: value computed is not used
[cdalten@localhost oakland]$ ./mall
tes
thi
inpu
dat
[cdalten@localhost oakland]$
.
- Follow-Ups:
- Re: invalid lvalue in assignment
- From: Spiros Bousbouras
- Re: invalid lvalue in assignment
- From: Default User
- Re: invalid lvalue in assignment
- References:
- invalid lvalue in assignment
- From: Chad
- Re: invalid lvalue in assignment
- From: Keith Thompson
- Re: invalid lvalue in assignment
- From: Chad
- Re: invalid lvalue in assignment
- From: Default User
- Re: invalid lvalue in assignment
- From: Chad
- Re: invalid lvalue in assignment
- From: Default User
- invalid lvalue in assignment
- Prev by Date: Re: Whgere can I find Factorial source code?
- Next by Date: Re: invalid lvalue in assignment
- Previous by thread: Re: invalid lvalue in assignment
- Next by thread: Re: invalid lvalue in assignment
- Index(es):
Relevant Pages
|