Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Pointers in C

Name: t hate me 2011-11-08 19:55

I'm learning C this year in school, and I'm having trouble with this program dealing with pointers. You guys probably get this alot, but could you please help me?

The program should do the following:

The user types a series of words and the program prints the second word the user typed.

   1. There is no space at the beginning
   2. There is no space at the end
   3. There is only one space between words

I can't get this to work using pointers(which we have to use). Here is my code.

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

int getString(char *p);
char *getSecondWord(char *p);
int printSecondWord(char *secondWordPointer);

main()
{
    char *p;
    char *secondWordPointer;
    int strLength = 80;
    p = malloc((strLength + 1) * sizeof(*p));

    getString(p);
    secondWordPointer = getSecondWord(p);

    printf("\nThe second word you typed was: ");

    printSecondWord(secondWordPointer);

    free(p);
}

int getString(char *p)
{
    int ndx = 0;

    while((*(p + ndx) = getchar()) != '\n')
       ndx++;
}

char *getSecondWord(char *p){

   
  char s[50];
  char *a  = &s[0];
    int ndx = 0;
    int count = 0;

    while(*(p + ndx) != ' ')
       ++ndx;

  
     while(*(p + ndx) != ' '){
     s[count] =  *(p + ndx) ; 
       ++ndx;
       count++;
    
     }

       return a;
}
 

int printSecondWord(char *secondWordPointer){


  printf("%s\n", *secondWordPointer);






I'm using the GCC compiler working in Ubuntu.
I have no compilation errors. Just a null output in the terminal.
Thanks.

Name: Anonymous 2011-11-08 21:04

>>32
printf("%s") is what is known as a 'semantic' error. Do you know why?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List