• C

copying value to char *fieldname

hi,

how to do you assign
a value to a type of
char *fieldname[1];

can I do:
*fieldname[0] = "xxxxxx";  ??
or use strcpy:
strcpy ( &fieldname[0], "xxxxxx");

pls help! a bit rusty with pointers..
a explantion would be great too

:)




thiamwahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

deightonprogCommented:
char *fieldname[1];

is an array of pointers to strings with two elements.

fieldname[0] = malloc(strlen(sstring) + 1);
strcpy(fieldname[0],sstring);

would make fieldname[0] a pointer to a block of memory containing the contents of sstring

whereas

fieldname[0] = sstring

would put a pointer to sstring in fieldname[0]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jhanceCommented:
char *fieldname[1];

is a declaration that creates an array of 1 pointers to a char.  It's the same as saying:

char *fieldname

In either case, you get storage for 1 char *.

You can then say:

fieldname[0] = "XXX";

This assigns the ADDRESS of the static string (or char array) "XXX" to the variable fieldname[0].

In your situation above, you CANNOT do:

strcpy ( &fieldname[0], "xxxxxx");

as there is NO STORAGE for the copied string at wherever fieldname[0] points to.  In order to do this yo umust first allocate space.  So you could do:

/* Allocate 256 bytes of storage */
fieldname[0] = (char *)malloc(256);

/* Now copy the string to the storage */
strcpy (fieldname[0], "xxxxxx");
0
jhanceCommented:
deighton,
you said:

>char *fieldname[1];

>is an array of pointers to strings
>with two elements.


This is NOT true!!  

char *fieldname[1]; DECLARES an array of pointers to _CHAR_ with _ONE_ element.  The only possible reference is to:

fieldname[0]



0
deightonprogCommented:
Taking a step back, are you really trying to utilise simple strings  which are arrays of caharacters

such as

main()
{

      char fieldname[8];
      
      strcpy(fieldname,"Experts");
      
      printf("\n%s",fieldname);
            
}      
0
deightonprogCommented:
oops
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.