6
char *tempMonth;

char month[4];
month[0]='j';
month[1]='a';
month[2]='n';
month[3]='\0';

how to assign month to tempMonth? thanks

and how to print it out finally?

thanks

hkvega
  • 305
  • 3
  • 8
  • 12

6 Answers6

12

In C, month == &month[0] (in most cases) and these equals a char * or character pointer.

So you can do:

tempMonth=month;

This will point the unassigned pointer tempMonth to point to the literal bytes allocated in the other 5 lines of your post.

To make a string literal, it is also simpler to do this:

char month[]="jan"; 

Alternatively (though you're not allowed to modify the characters in this one):

char *month="jan";

The compiler will automatically allocate the length of the literal on the right side of the month[] with a proper NULL terminated C string and month will point to the literal.

To print it:

printf("That string by golly is: %s\n", tempMonth); 

You may wish to review C strings and C string literals.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
the wolf
  • 34,510
  • 13
  • 53
  • 71
  • just some suggestions: change `=` to `==` in the first line and make it clear that `month == &month[0]` _mostly._ Not so when using sizeof or certain other language features. – paxdiablo Apr 18 '11 at 04:09
3

If you just want a copy of the pointer, you can use:

tempmonth = month;

but that means both point to the same underlying data - change one and it affects both.

If you want independent strings, there's a good chance your system will have strdup, in which case you can use:

tempmonth = strdup (month);
// Check that tempmonth != NULL.

If your implementation doesn't have strdup, get one:

char *strdup (const char *s) {
    char *d = malloc (strlen (s) + 1);   // Allocate memory
    if (d != NULL) strcpy (d,s);         // Copy string if okay
    return d;                            // Return new memory
}

For printing out strings in a formatted fashion, look at the printf family although, for a simple string like this going to standard output, puts may be good enough (and likely more efficient).

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2
tempMonth = month

When you assign a value to a pointer - it's a pointer, not a string. By assigning as above, you won't miraculously have two copies of the same string, you'll have two pointers (month and tempMonth) pointing to the same string.

If what you want is a copy - you need to allocate memory (using malloc) and then actually copy the values (using strcpy if it's a null-terminated string, memcpy or a loop otherwise).

littleadv
  • 20,100
  • 2
  • 36
  • 50
1
tempmonth = malloc (strlen (month) + 1); // allocate space
strcpy (tempMonth, month);               //copy array of chars

Remember to:

include <string.h>
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Freaktor
  • 700
  • 1
  • 10
  • 21
  • There's a buffer overflow waiting to happen. Always use `strncpy`, and make sure tempMonth points to properly allocated memory. – Andrew Cooper Apr 18 '11 at 03:58
  • I didn't downvote you but I think your allocation should be for `strlen(month)+1`. `sizeof(tempmonth)` is the size of the _pointer._ – paxdiablo Apr 18 '11 at 03:59
  • Fixed and upvoted. You know, you don't _have_ to leave your answers as they are if you want to change them (I think you can edit your own posts at _any_ rep level). – paxdiablo Apr 18 '11 at 04:17
1
#include "string.h" // or #include <cstring> if you're using C++

char *tempMonth;
tempMonth = malloc(strlen(month) + 1);
strcpy(tempMonth, month);
printf("%s", tempMonth);
Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81
0

You could do something like:

char *months[] = {"Jan", "Feb", "Mar", "Apr","May", "Jun", "Jul", "Aug","Sep","Oct", "Nov", "Dec"};

and get access

printf("%s\n", months[0]);
edgarmtze
  • 24,683
  • 80
  • 235
  • 386