[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

copying value to char *fieldname

Posted on 1999-11-25
5
Medium Priority
?
251 Views
Last Modified: 2010-04-15
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

:)




0
Comment
Question by:thiamwah
  • 3
  • 2
5 Comments
 
LVL 18

Accepted Solution

by:
deighton earned 140 total points
ID: 2233993
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
 
LVL 32

Expert Comment

by:jhance
ID: 2234005
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
 
LVL 32

Expert Comment

by:jhance
ID: 2234012
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
 
LVL 18

Expert Comment

by:deighton
ID: 2234026
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
 
LVL 18

Expert Comment

by:deighton
ID: 2234030
oops
0

Featured Post

Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question