473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Need help: Pointer to String

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
Nov 14 '05 #1
6 12657

Please refer to http://www.eskimo.com/~scs/C-faq/q6.8.html
I belive it is answer on your question.

Regards,
Sergey
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

Nov 14 '05 #2
"PX" <ry*****@yahoo.com> wrote in message
news:68**************************@posting.google.c om...
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
Nov 14 '05 #3
Greetings.

In article <68**************************@posting.google.com >, PX wrote:
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

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= <> In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Nov 14 '05 #4
PX wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
PX <ry*****@yahoo.com> wrote:
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.

--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #6
Alex Monjushko<mo*******@hotmail.com> wrote in message news:<c0*************@ID-190529.news.uni-berlin.de>...
PX <ry*****@yahoo.com> wrote:
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, ki**********@mail.ru
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
4
by: kazack | last post by:
I posted a similiar question in this newsgroup already and got an answer which I already knew but didn't get the answer I was looking for so I am reposting the code and question differently in the...
3
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store...
5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
14
by: key9 | last post by:
Hi All On coding , I think I need some basic help about how to write member function . I've readed the FAQ, but I am still confuse about it when coding(reference / pointer /instance) , so I...
19
by: ash | last post by:
hi friends, i have some questions whch is in my last year question papers.i need some help to get logic of these questions. 1) write a C function, that takes two strings as arguments and...
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
0
by: akshaycjoshi | last post by:
I am reading a book which says Even though unboxed value types don't have a type object pointer, you can still call virtual methods (such as Equals, GetHashCode, or ToString) inherited or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.