Jump to content

help with initializing a char array

- - - - -

  • Please log in to reply
7 replies to this topic

#1
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
is it possible to initialise a char array separately from where it is declared without using either a for loop or initialising the array character by character?

#2
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
char *str;


str = "hello";


#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Here are some of varying levels of usefulness and complexity:
char foo1[] = "abc";                         //automatically define null ending
char foo2[4] = "abc\0";                    //manually define it
char foo3[4] = {'a', 'a' + 1, 'c', 0}; //automatic conversion to chars, then char array
printf("%s, %s, %s\n", foo1, foo2, foo3);
The third one works because ASCII 'a' + 1 is ASCII 'b', and 0 is the same as char \0 (but not '0')
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
csepraveenkumar

csepraveenkumar

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts

Alexander said:

Here are some of varying levels of usefulness and complexity:
char foo1[] = "abc";                         //automatically define null ending
char foo2[4] = "abc\0";                    //manually define it
char foo3[4] = {'a', 'a' + 1, 'c', 0}; //automatic conversion to chars, then char array
printf("%s, %s, %s\n", foo1, foo2, foo3);
The third one works because ASCII 'a' + 1 is ASCII 'b', and 0 is the same as char \0 (but not '0')
in this example all the arrays are initialised where they are declared. what if i want to initialise a char array some place other than where i declare it without using for loop.

#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
Use strncpy()


char[10] str;

...

strncpy(str, "Hello", 10);


Just be sure to include string.h

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
The most simple way is to access it as a pointer, an example showing this:
  char somestring[32];
  char *ptr = somestring;
  if(!ptr) {
    fprintf(stderr, "Pointing to NULL buffer");
  } 
  strncpy(ptr, "foobar\0", sizeof(somestring));

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#7
rocketboy9000

rocketboy9000

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
WARNING:
gregwarner wrote:
char[10] str;
This is not valid C, it is Java. In C, the array type specifier has the same syntax, precedence and notation as the array subscript operator.
char str[10];


#8
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
So sorry about that. You're absolutely right. I code in Java at work, and with my head in the Java clouds all day long, it's easy to accidentally cross between the two languages. Good spotting my mistake. :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users