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: t hate me 2011-11-08 19:58

I'm guessing I have totally wrong code in the getSecondWord function because I have no idea what to put there. So that was pretty much a guess.

Name: Anonymous 2011-11-08 20:00

Well if this can help, the bottom part should be
int printSecondWord(char *secondWordPointer){


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


}

print("%s") expects a string of characters, that is a char*, you are giving it a char by writing *secondWordPointer.

Name: Anonymous 2011-11-08 20:04

Also, why create a specific printSecondWord function when you could just use puts for that ?

Name: Anonymous 2011-11-08 20:05

This is my output, haha. ䷨!Ŀ��������!Ŀ

Name: Anonymous 2011-11-08 20:06

>>4

Becuase I'm a complete noob, and we havn't learn that yet.

Name: Anonymous 2011-11-08 20:06

If you want to do it right, write something smart and simple such as e.g.
#include <stdio.h>

int main(int argc, char** argv)
{
    char c;

    /* inhibit first word + first space */
    while(getchar() != ' ');

    /* write out second word */
    while((c = getchar()) != ' ')
        putchar(c);   

    return 0;
}

Name: Anonymous 2011-11-08 20:11

>>7

I wish I could, but the teacher wants to see it exactly this way. It's dumb I know.

Name: Anonymous 2011-11-08 20:12

>>8
OK. I'm currently tring to fix your program. Managed to fix getString() so far.

Name: Anonymous 2011-11-08 20:15

>>7

Although I completely understand your code. I'm not sure what this is (int argc, char** argv). Why is that needed?

Name: Anonymous 2011-11-08 20:16

I notice one mistake is that getSecondWordPointer returns a pointer to a character array allocated on that function's stack. The thing is, that array will be destroyed when the function returns. You should write e.g. char *s = malloc(50);

Name: Anonymous 2011-11-08 20:17

>>9

Thanks so much.

Name: Anonymous 2011-11-08 20:22

Fixed version, with FIX written were changes were made:
#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 = malloc(50);    /* FIX ! */
  char *a  = s;    /* FIX ! */
    int ndx = 0;
    int count = 0;

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

    ++ndx;    /* FIX ! */

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

       return a;
}
 

int printSecondWord(char *secondWordPointer){


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


}

Name: Anonymous 2011-11-08 20:23

Untested, might have mistakes.

char *p = buffa;
while (p++ != ' ');
for (char *q = p; ++q != ' ' || q=0; );
puts(p);

Name: Anonymous 2011-11-08 20:25

>>14
Missing a few dereferences over there.

Name: Anonymous 2011-11-08 20:25

>>13

Thanks, your seriously the best. At least I kinda had the right idea. haha

Name: Anonymous 2011-11-08 20:26

>>10
Just typical C practice. It allows for command line programs to have arguments.

Name: Anonymous 2011-11-08 20:27

>>3
print("%s") expects a string of characters, that is a char*, you are giving it a char by writing *secondWordPointer.

That is incorrect you fucking nigger. Again, for the second time, C doesn't have a string of characters. Instead, what C has are an array of characters. If this array of characters is terminated with '\0', then it becomes a string.

Name: Anonymous 2011-11-08 20:29

>>16
Great. As a final note, you should probably free(secondWordPointer); at the end of main to avoid memory leaks.

Name: Anonymous 2011-11-08 20:29

>>3
Also, take your highlighted incorrect usage and shove it straight up your ass you fucking uneducated faggot. You never have and never will amount to anything more than some general labor monkey that works form some shit no name hick firm.

Name: Anonymous 2011-11-08 20:30

>>19
Did your nigger ass even bother to look at the code? Cripes, shut up and go scrub another toilet you fucking dumb nigger bitch.

Name: Anonymous 2011-11-08 20:31

>>18
Sorry, I confused two similar terms.
If you want to be pedantic, you made a grammar mistake:
what C has are

Name: Anonymous 2011-11-08 20:32

>>22
This is why you don't work as a computer programmer. Now go run along and suck another dick you fucking faggot.

Name: Anonymous 2011-11-08 20:32

>>20

GTFO

Name: Anonymous 2011-11-08 20:33

>>24
Go learn what C is before you mislead another person you fucking uneducated nigger.

Name: Anonymous 2011-11-08 20:36

>>25
OK, but please understand that there is little semantic difference between "array" and "string".

Name: kodak_gallery_programmer !!V98H8hw9ydTWLgo 2011-11-08 20:40

>>26
OK, but please understand that there is little semantic difference between "array" and "string".

That is totally incorrect. An array in C is an immutable lvalue. In contrast, a string can either be a mutable or immutable array of characters that get terminated with '\0'.

Name: kodak_gallery_programmer !!VIk1pgCZf9P/QBQ 2011-11-08 20:42

>>27
And is why you have a bunch of libraries in C just to handle basic string manipulations.

Name: Anonymous 2011-11-08 20:44

>>27
As an indeed uneducated person, I find that though they have special meanings in the context of C programming, their general meanings are very close.

Name: kodak_gallery_programmer !!VIk1pgCZf9P/QBQ 2011-11-08 20:46

>>29
No they aren't. And the fact that you think they are makes you that much dumber. Either learn the fundamental differences or just don't bother taking the time to learn about programming.

Name: Anonymous 2011-11-08 20:46

Anyway amidst the flames and the trolls I guess as an uneducated teenager wasting his time on 4chan I have learnt the hard way that "strings" are always null-terminated "arrays".

Name: Anonymous 2011-11-08 20:49

Besides, to go back to the stupid post that created this whole debacle, it can be said that printf("%s"); actually expects a string, because a non-null-terminated array would make it crash or print garbage

Name: Anonymous 2011-11-08 20:50

>>31

I learn more on here then in class. Kinda sad.

Name: Anonymous 2011-11-08 20:53

Thanks again guys. You really helped me out.

Name: Anonymous 2011-11-08 21:04

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

Name: Anonymous 2011-11-08 21:10

>>35

No. Why?

Name: Anonymous 2011-11-08 21:21

One problem I found was that if only two words are entered.

Name: Anonymous 2011-11-08 21:28

>>36
If you don't know, why do you insist on giving out moronic advice on shit you don't understand?

Name: Anonymous 2011-11-08 21:33

int main(){
  char *c; //here, I used a pointer
  char str[40];
  scanf("%s", str);
  scanf("%s", str);
  printf(str);
  return 0;
}

Name: Anonymous 2011-11-08 22:47

>>19
at the end of main
No, that's useless since the entire process is gone once main exits. free() is for use... deeper inside, where memory that isn't needed can be reused for something else.


>>39
printf(str)
Oops.

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