Need help: Pointer to String

P

PX

Greetings,

Please take a look at following code:
"

struct STR {
char string[32];
... /* something else */
};

int main(int argc, char *argv[])
{
struct STR *ptr;
ptr->string = argv[2];

}
"

The compiler complained "invalid lvalue in assignment" when I tried to
compile. I am wondering why a char pointer can work fine but
ptr-string can't. Is there any fast method that can get this done
instead of writing a for loop to assign characters one by one?

Thanks a bunch!

Regards,
PX
 
M

Mike Wahler

PX said:
Greetings,

Please take a look at following code:
"

struct STR {
char string[32];
... /* something else */
};

int main(int argc, char *argv[])
{
struct STR *ptr;
ptr->string = argv[2];

}
"

The compiler complained "invalid lvalue in assignment" when I tried to
compile.

Good thing, too. If you'd got it to compile, running
it would have invoked undefined behavior, due to your
dereference of an invalid pointer (you never initialized
or assigned it a valid value). You also didn't create
any type 'struct STR' objects, so you can't assign
the address of one to your pointer.

I am wondering why a char pointer can work fine but
ptr-string can't.

Apparently you did the former correctly. You're obviously
not doing the latter correctly.

Arrays cannot be assigned to.

A pointer must contain the address of an existing object
in order for a dereference of that pointer to be valid.
Is there any fast method that can get this done
instead of writing a for loop to assign characters one by one?

Use the standard library, Luke!


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

#define SZ 32

struct STR
{
char string[SZ];
};

int main(int argc, char **argv)
{
struct STR ss = {0};
struct STR *ptr = &ss;

if(argc > 2 && strlen(argv[2]) < SZ)
strcpy(ptr->string, argv[2]);
else
{
printf("Argument 2 missing or longer than %d characters,\n"
" program terminating\n", SZ - 1);

return EXIT_FAILURE;
}

printf("ptr->string contains: %s\n", ptr->string);

/* etc */

return 0;
}


I don't know why you feel you need to use a pointer, though.

strcpy(ss.string, argv[2]);

-Mike
 
T

Tristan Miller

Greetings.

Greetings,

Please take a look at following code:
"

struct STR {
char string[32];
... /* something else */
};

int main(int argc, char *argv[])
{
struct STR *ptr;
ptr->string = argv[2];

}
"

The compiler complained "invalid lvalue in assignment" when I tried to
compile. I am wondering why a char pointer can work fine but
ptr-string can't. Is there any fast method that can get this done
instead of writing a for loop to assign characters one by one?

You have two problems here: first, ptr is an uninitialized pointer, which
means that no storage has been allocated for a structure. Even if the
assignment you're trying were allowed, the program would crash or result in
undefined behaviour when run because ptr doesn't actually point to
anything. You need to first use malloc() to create some space for a
structure, or declare some struct STR foo and have ptr point to that with
ptr = &foo.

The second mistake is that ptr->string is an array with fixed storage; you
can't arbitrarily make it point to something else (in this case, argv[2]).
You could either change the type of ptr->string to a character pointer and
leave the assignment as-is, or you could change the assignment to a call to
strcpy(). Exactly which method you choose depends on what the program is
trying to do; in particular, the strcpy() technique will work only if
you're sure argv[2] will never exceed 32 characters in length (including
the terminating null character).

Regards,
Tristan
 
C

CBFalconer

PX said:
struct STR {
char string[32];
... /* something else */
};

int main(int argc, char *argv[])
{
struct STR *ptr;
ptr->string = argv[2];
}

The compiler complained "invalid lvalue in assignment" when I tried
to compile. I am wondering why a char pointer can work fine but
ptr-string can't. Is there any fast method that can get this done
instead of writing a for loop to assign characters one by one?

argv[2] is of type pointer to char (char *). ptr->string is of
type array[32] of char. These are not the same, even though the
array can often be designated by a pointer to its first element,
i.e. of type pointer to char.
 
A

Alex Monjushko

PX said:
Greetings,
Please take a look at following code:
"
struct STR {
char string[32];
... /* something else */
};
int main(int argc, char *argv[])
{
struct STR *ptr;
ptr->string = argv[2];

The compiler complained "invalid lvalue in assignment" when I tried to
compile. I am wondering why a char pointer can work fine but
ptr-string can't. Is there any fast method that can get this done
instead of writing a for loop to assign characters one by one?

First, you have not initialized 'ptr'. You will need to allocate
memory for it by using 'malloc'.

ptr = malloc(sizeof *ptr);

if(!ptr)
{
/* could not get memory */
}

Second, the assignment is indeed invalid. You must use strcpy or
somesuch to copy the contents of argv[2] into ptr->string. Make
sure that your array is big enough to hold argv[2].

if(strlen(argv[2]) < sizeof ptr->string)
{
strcpy(ptr->string, argv[2]);
}
else
{
/* argv[2] is too big */
}


Finally, you have not bothered to check 'argc' to make sure
that argv[2] points to something worthwhile in the first place.

if(argc < 3)
{
/* not enough arguments were supplied */
}

Don't forget to include <stdlib.h> for malloc and <string.h>
for strcpy.
 
O

Oleg Melnikov

Alex Monjushko said:
PX said:
Greetings,
Please take a look at following code:
"
struct STR {
char string[32];
... /* something else */
};
int main(int argc, char *argv[])
{
struct STR *ptr;
ptr->string = argv[2];

The compiler complained "invalid lvalue in assignment" when I tried to
compile. I am wondering why a char pointer can work fine but
ptr-string can't. Is there any fast method that can get this done
instead of writing a for loop to assign characters one by one?

First, you have not initialized 'ptr'. You will need to allocate
memory for it by using 'malloc'.

ptr = malloc(sizeof *ptr);

if(!ptr)
{
/* could not get memory */
}

Second, the assignment is indeed invalid. You must use strcpy or
somesuch to copy the contents of argv[2] into ptr->string. Make
sure that your array is big enough to hold argv[2].

if(strlen(argv[2]) < sizeof ptr->string)
{
strcpy(ptr->string, argv[2]);
}
else
{
/* argv[2] is too big */
}


Finally, you have not bothered to check 'argc' to make sure
that argv[2] points to something worthwhile in the first place.

if(argc < 3)
{
/* not enough arguments were supplied */
}

Don't forget to include <stdlib.h> for malloc and <string.h>
for strcpy.

The key moment (heart of the matter) is to use strcpy, not just '='.
C style - using strcpy for string assignment. But C++ allows to
overloade operator '='. Using standalone '=' without strcpy in C is bug.
Sorry for my bad English. Melnikov Oleg, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,355
Latest member
MavoraTech

Latest Threads

Top