简体   繁体   中英

Pointer to the first character of a character array?

EDIT: Apparently I had to declare char *first at the beginning of the pigLatin method and initialize it to &word[counter] later in the method. Anybody knows why this is? I'm using Visual Studio 2010.

I'm having trouble figuring out why this gives me a compile-time error. The code in question:

#include <stdio.h>
#include <string.h>
#include <conio.h>

char *pigLatin(char *word)
{
   if (word[0] == 'a' || word[0] == 'e' || word[0] == 'i'
           || word[0] == 'o' || word[0] == 'u')
   {
       char yay[] = "yay";
       strcat(word, yay);
       return word;
   }
   else
   {
       int length = strlen(word);
       int counter = 0;
       char addOn[] = "";
       char remainder[] = "";
       char yay[] = "yay";
       printf("%s", yay);

       char *first = &word[counter]; 
       printf("%c", *first); // error is here, don't know why it doesn't print
       return word;
   }
}


int main()
{
   char hello[] = "hello";
   pigLatin(hello);
   printf("%s", hello);
   getch();
   return (0);
}

1>------ Build started: Project: Program_One, Configuration: Release Win32 ------

1> programone.c

1>programone.c(12): warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

1>programone.c(24): error C2143: syntax error : missing ';' before 'type'

1>programone.c(25): error C2065: 'first' : undeclared identifier

1>programone.c(25): error C2100: illegal indirection

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I don't see why my pointer to the first character of the array "hello" isn't printing correctly.

Thanks in advance !

You are not assigning your pointer variable to point at the address of the first character. You are assigning the value of the character itself to the pointer variable, which is why the compiler errors.

You need to change this line:

char *first = word[counter]; 

To this:

char *first = &word[counter]; 

Or, just do this instead:

char *pigLatin(char *word)   
{   
    int counter = 0;   
    printf("%c", word[counter]);
    return word;   
}   

Update: even if the code did compile, it is dangerous. The compiler warning about strcat() is valid. You have not allocated enough memory to append "yay" to the input word if it starts with a vowel. To really make this code safer, you should use the std::string class instead of raw pointers:

#include <conio.h> 
#include <string> 

std::string pigLatin(const std::string &word) 
{ 
    switch( word[0] )
    {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u': 
            return word + "yay"; 
    }

    int length = word.length(); 
    int counter = 0; 
    //...

    printf("%c", word[counter]);
    return word; 
} 

int main() 
{ 
   std::string word = pigLatin("hello");
   printf("%s", hello.c_str()); 
   getch(); 
   return 0; 
} 

This line here:

char *first = word[counter]; 

Should be this one instead:

char *first = &word[counter];

Due to you pointing at a value where the pointer targets, instead of the memory address itself, so to get the actual value you need the ampersand sign in front of the call to the array.

You are currently telling the compiler to point first to the value of word[counter] which means it's pointing at somewhere strange in memory. You want to be pointing at the first element of the array so you need to replace:

char *first = word[counter];

with:

char *first = &word[counter];

or:

char *first = word;

Edit - The compiler error you mention in your comments relates to lines 23 & 24 of your source code. There are less than 23 lines in the example you posted above. The eror must therefore be in some extra code you've not posted here...

Edit2 - With a couple of minor adjustments, your code looks like it works to me: http://codepad.org/73BKMSgv

char str[] = "test";
char* p;
p = str;
printf(p[0]);

Output:
t

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM