tags:

views:

95

answers:

4

AFunc changes what was sent to it, and the printf() outputs the changes:

void AFunc ( char *myStr, int *myNum )
{
    *myStr = 's';
    *myNum = 9;
}


int main ( int argc, char *argv[] )
{
   char someString = 'm';
   int n = 6;

   AFunc(&someString, &n);

   printf("%c" "%d", someString, n);
}

But what if the string was more than one char? How would the code look differently? Thanks for any help.

+1  A: 

You are only dealing with chars and char pointers. None of the char pointers are valid strings as they are not null terminated.

Try defining a string and see what it looks like.

leppie
+3  A: 

If it were a "string" instead of a char, you would do something like this:

#include <stdio.h>

void AFunc (char *myStr, int *myNum) {
    myStr[0] = 'p'; // or replace the lot with strcpy(myStr, "pax");
    myStr[1] = 'a';
    myStr[2] = 'x';
    myStr[3] = '\0';
    *myNum = 9;
}

int main (void) {
    char someString[4];
    int n = 6;

    AFunc(someString, &n);

    printf("%s %d", someString, n);
    return 0;
}

which outputs:

pax 9

A "string" in C is really an array of characters terminated by the \0 (NUL) character.

What the above code does is to pass in the address of the first character in that array and the function populates the four characters starting from there.

paxdiablo
+2 ??? I dont follow your code, typo? did you mean ++ ?
leppie
@leppie, `*(x+2)` is identical to `x[2]` - I've changed it to the easier-to-understand version although I realise you probably know that, it's just that my original were all `*(x+2)` due to a cut'n'paste error on my part.
paxdiablo
@paxdiablo: Yeah, that one, I probably jumped the gun while you were editing :)
leppie
Thank you all for your answers. Every one of them were useful for me.
+1  A: 
    But what if the string was more than one char? How would the code look 
differently? Thanks for any help

Ofcourse, you would modify the other characters as well, but in the exact same way you did the first time.

  1. Declare a char array and pass its address
  2. Modify values at those address

A char array would be a more clear term for a string.

Praveen S
Thank you for this . This helped.
+3  A: 

In C, a pointer to char isn't necessarily a string. In other words, just because you have char *x;, it doesn't mean that x is a string.

To be a string, x must point to a suitably allocated region which has a 0 in it somewhere. The data from the first character that x points to and up to the 0 is a string. Here are some examples of strings in C:

char x[5] = {0}; /* string of length 0 */
char x[] = "hello"; /* string of length 5, the array length being 6 */
char *x = "hello"; /* string of length 5.  x is a pointer to a read-only buffer of 6 chars */

char *x = malloc(10);
if (x != NULL) {
    strcpy(x, "hello"); /* x is now a string of length 5.  x points
                           to 10 chars of useful memory */

}

The following are not strings:

char x[5] = "hello"; /* no terminating 0 */
char y = 1;
char *x = &y; /* no terminating 0 */

So now in your code, AFunc's first parameter, even though is a char * isn't necessarily a string. In fact, in your example, it isn't, since it only points to a memory that has one useful element, and that's not zero.

Depending upon how you want to change the string, and how the string was created, there are several options.

For example, if the myStr points to a writable memory, you could do something like this:

/* modify the data pointed to by 'data' of length 'len' */
void modify_in_place(char *data, size_t len)
{
    size_t i;
    for (i=0; i < len; ++i)
        data[i] = 42 + i;
}

Another slightly different way would be for the function to modify data until it sees the terminating 0:

void modify_in_place2(char *data)
{
    size_t i;
    for (i=0; data[i]; ++i)
        data[i] = 42 + i;
}
Alok
Thank you Alok.