Storing a hostname in an array

Posted on 2005-02-27
Medium Priority
Last Modified: 2010-04-15
Hello y'all!

I know that C does not have the ability to use strings, only arrays of characters. I want to store a web address in an array for a client to access later on, how would i go about hard coding this address?

Thanks in advance!
Question by:coollizard
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Expert Comment

ID: 13413856
You can define global variable:

char* sAddress = "http://www.experts-exchange.com";

and use it in any place in the program.

LVL 55

Expert Comment

by:Jaime Olivares
ID: 13414152
There are many things to consider...
Is your array a fixed array? If so, that means you can store just a fixed quantity of web addresses.
Maybe a linked list will be better.

If you will store a string into an array, you will need an element counter and maybe to store a copy of the string, since original string can disappear from memory. Something like this:

AddAddress(char **array, int index, char *theString)
        char *strCopy = malloc(strlen(theString)+1);
        strcpy(strCopy, theString);

        array[index] = strCopy;  /* store a copy of original string */

Notice you will need to scan all array at program end to free all strings allocated with malloc().
Good luck,

Expert Comment

ID: 13416992
Note that

>char* sAddress = "http://www.experts-exchange.com";

will let the actual characters 'h' 't' 't' 'p' etc. be stored in an arbitary place in memory, usually somewhere read-only. Modifying the location where sAddress points to, might cause problems:

char* sAddress = "http://www.experts-exchange.com";
sAddress[0] = 'f'; /* Bad */
strcpy(sAddress, "http://somewhere-else"); /* Bad */

To actually create an array and copy characters inside..... Well, you create an array and copy characters inside:

char address[100]; /* Maximum length = 99, need the last char to be '\0' */
strcpy(address, "http://www.experts-exchange.com");

Or, just initialize the array with the string:

char address[100] = "http://www.experts-exchange.com"; /* Some characters will be wasted */
char address[] = "http://www.experts-exchange.com"; /* An array of just sufficient size to store the characters in the URL plus one for the terminating '\0' will be created, you will not be able to store larger URLs */

Accepted Solution

madhurdixit earned 2000 total points
ID: 13417514
coollizard ,

You need to decide the maximum size which you would allow for the address strings.
In this case sounds like , you have got only one address, this sounds even simpler.

Say the address is web_addr.

you can declare it as

#define  MAX_SIZE    200 //I am allowing 200 characters for the web address string.

main( )
 char *web_addr;
 web_addr = (char *) malloc (sizeof(char)*MAX_SIZE);
 strcpy( web_addr , "www.experts-exchange.com");

Using malloc will allocate memory from heap. So no issues on memory.

An alternative would be :

main ( )

char web_addr[MAX_SIZE];
strcpy( web_addr , "www.experts-exchange.com");

Here the memory will be from stack, since we want larger chunks of memory , I would suggest the first approach of using


Author Comment

ID: 13418400
Thats great man, thanks a lot! The malloc way is definately the more efficient way of doing things. Cheers!

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

765 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