1

Okay, long story short I am having some trouble: I'm trying to send the starting address of a character array (string) to a function. Once the function gets the pointer, I'd like the function to parse through the characters one by one (in order to modify certain characters). As of right now I just set it up to print each character, but I'm still failing at that really badly.

This is what I have:

#include "system.h"
#include <stdio.h>

typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;

void EXCLAIM(char *msg){
    char the_char = 0;
    the_char = msg;
    while (the_char != 0)
    {
        printf(the_char);
        *msg = *(msg++);
        the_char = *msg;
    }
}

int main(void) {
    char *first_str = "This is a test. Will this work. I. am. Not. Sure...";
    while (1) {
        EXCLAIM(first_str);
    }
}

EDIT:

Here is the updated code with what I was trying to do; send the pointer and go through each character replacing all periods with exclamation marks.

#include "system.h"
#include <stdio.h>

typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;

void exclaim(char *msg){
    int i;
    for( i=0; msg[i]; i++ )
    {
        if (msg[i] == '.') {
            msg[i] = '!';
        }
    }
printf(msg);
}

int main(void) {
    char *the_sentences = "This is a test. Will this work. I. am. Not. Sure...";
    while (1) {
        exclaim(the_sentences);
    }
}

Thank you all for your help!

5 Answers 5

1

Your main() is fine.

The issue lies in the pointer arithmetics inside EXCLAIM.

I can see two different versions that are useful:

Using pointer arithmetics

while (*msg)
{
    printf("%c", *msg);
    msg++
}

Using indexing

int i;
char the_char;
for( i=0; msg[i]; i++ )
{
    printf( "%c", msg[i] );
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's perfect, now if I wanted to test the character in there to say change periods to exclamation points, would it be as simple as: char test_char = "%c", msg[i]; if (test_char = '.') { msg[i] = '!'; }
The "%c", is an argument to the function 'printf', and not really related to the indexing as such. So, even simpler: if( msg[i] == '.' ) msg[i] = '!';
1

Try something like this:

#include <stdio.h>

void exclaim(char * s)
{
    while (*s != '\0')
    {
        putc(*s);
        ++s;
    }
}

Notes:

  • Don't shout. Don't use all caps for anything but preprocessor macros.

  • Print characters, not strings.

  • No need to make another copy. The function argument is already a local variable that you can use directly.

Comments

1

You should use:

  the_char = *msg;

not:

  the_char = msg;

Comments

1
*msg = *(msg++); 

What are you trying to do with this line of code? Do you understand what it actually does? First off, you need to know that msg++ changes the value of the pointer. Then you assign whatever it USED to point to to the new location. This means that the whole string will be replaced with copies of the first character in the string.

The moral of the story is to not do too much in a single line of code. You have to be especially careful with the increment operator ++. Even seasoned programmers typically put something like msg++ in its own line because it gets too complicated when you try to mix it in with a complex expression.

Comments

1

Something that was not mentioned but, attemping to modify a string literal is undefined behavior.

You need to change the following line from,

char *the_sentences = "This is a test. Will this work. I. am. Not. Sure...";

to an array.

char the_sentences[] = "This is a test. Will this work. I. am. Not. Sure...";

Then feel free to modify the contents inside your function.

#include <stdio.h>

void foo(char *msg)
{
    for (; *msg; msg++)
        if (*msg == '?')
            *msg = '!';
}

int main(void)
{
    char line[] = "Hello, world?? How are you tonight??";

    foo(line);

    puts(line);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.