user225626
user225626

Reputation: 1099

C101--string vs. char:

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.

Upvotes: 0

Views: 280

Answers (4)

Alok Singhal
Alok Singhal

Reputation: 96251

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;
}

Upvotes: 3

paxdiablo
paxdiablo

Reputation: 882766

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.

Upvotes: 3

Praveen S
Praveen S

Reputation: 10393

    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.

Upvotes: 1

leppie
leppie

Reputation: 117360

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.

Upvotes: 1

Related Questions