3 Replies - 1122 Views - Last Post: 17 November 2013 - 07:57 PM Rate Topic: -----

#1 whitaker144   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 58
  • Joined: 20-September 13

Trouble with pointer char modification and malloc

Posted 17 November 2013 - 02:51 PM

Hello, I'm having trouble creating a program (it is a school assignment) that will take a word via command line argument, and convert it to Pig Latin. For example if I have the word "hello", it will turn into "ellohay".

Other than that we were given the function prototype:
char * PigLatin( char * piglatin, char *word );
as well as told we CANNOT use the string.h library at all. I researched and used google/forums like these for around a day now. Everybody says pointer chars are "string literals" and cannot be edited at all. Additionally I tried copying the pointer char into an array char, but was told that's not possible unless using "strcpy" which is of course a string.h library.

If this is the case, how am I supposed to modify my char * word or modify my char * piglatin, I've tried using malloc as the teacher said that we will need to allocate new memory for the Pig Latin word.

Here is my code so far, seeing as I can't use any string.h libraries, I made my own "string length" method to count the characters in the string:

/**************************************************************************/
/* Declare include files
 **************************************************************************/
#include <stdio.h>
#include <stdlib.h>

/**************************************************************************/
/* Function prototypes
 **************************************************************************/
char * PigLatin( char * piglatin, char *word );
/**************************************************************************/
int i = 0;

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

	if ( argc != 2 ) {
		
		printf("Invalid number of command-line arguements.");
		return EXIT_FAILURE;
	}
	
	else {
		printf("Original string is: %s\n",argv[1]);
		PigLatin("ay",argv[1] );

	}
	return 0;
}

char * PigLatin( char * piglatin, char *word ) {
	printf("Word argument is: %s\n",word);
	piglatin = malloc(sizeof(*word + 2));
	piglatin = *word + "ay";
	printf("%s\n",piglatin);
	string_length(piglatin);
	printf("Length is: %d\n",i);
	return word;
}

int string_length(char s[]) {

	while(s[i] != '\0') {
		i++;
	}
	return i;


}



I don't get any errors and it compiles properly, but for example if I had the command line argument "hello", the following would display:

Original string is: hello
Word argument is: hello
�0 (weird characters)
Length is: 3

Then if I somehow solve this problem, I'll still have to figure out how to get specific characters WITHIN the string. For example, I'll have to figure out if I had the word "hello", how to move the "h" to the end of the word and apply "ay".

I'd really appreciate any help I can get!

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with pointer char modification and malloc

#2 Salem_c   User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 2186
  • View blog
  • Posts: 4,250
  • Joined: 30-May 10

Re: Trouble with pointer char modification and malloc

Posted 17 November 2013 - 03:26 PM

> 33 piglatin = *word + "ay";
You need to write your own version of strcpy() and strcat() as well.

> Everybody says pointer chars are "string literals" and cannot be edited at all.
Consider this example.
$ cat foo.c
#include<stdio.h>

int string_length(char s[]) {
  int i = 0;
  while(s[i] != '\0') {
    i++;
  }
  return i;
}

int main()
{
  char  *a = "hello";
  char  b[] = "hello";
  char  c[100] = "hello";
  printf("Length of a = %d\n", string_length(a) );
  printf("Length of b = %d\n", string_length(B)/> );
  printf("Length of c = %d\n", string_length(c) );
  return 0;
}
$ gcc -Wall -Wextra foo.c
$ ./a.out 
Length of a = 5
Length of b = 5
Length of c = 5


The string that a points to cannot be modified, though a could be made to point somewhere else (including pointing at some memory you can modify, say through using malloc).
The string that b contains can be modified, but you can't make it longer.
The string that c contains has plenty of room to append more characters.

I know it sounds simplistic, but whenever you see a * (as a pointer), you need to be thinking "what memory is this pointing to, how large is that memory, and can I write to it".

As you can see above, there are many ways of declaring the memory to pass to the string_length() function.
Was This Post Helpful? 0
  • +
  • -

#3 whitaker144   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 58
  • Joined: 20-September 13

Re: Trouble with pointer char modification and malloc

Posted 17 November 2013 - 03:40 PM

So I suppose my next question is how do I know how much space I need, if my word will always be a different length? Thanks to what you mentioned I actually somewhat have it working, but a couple new issues have come up. I didn't change my main or string_lengthh method, just my PigLatin so I'll post what I updated about my PigLatin:

char * PigLatin( char * piglatin, char *word ) {
	string_length(word);
	piglatin = malloc(i * sizeof(char));
	word[i] = word[0];
	word[0] = ' ';
	word[i+1] = 'a';
	word[i+2] = 'y';
	printf("Pig Latin word is: %s   \n",word);
}


So this actually SORT OF produces my desired result: "ellohay", but the problems are: something else prints right after that says exactly this "H_AGENT_PID=1695". I googl'd what this means, and all I found was articles about "SSH_AGENT_PID=1695" and not a single result/post about this occuring in C programming, is this due to something I've messed up in memory?

Secondly, if I allocate memory for "word" instead of piglatin, or even add in malloc for word, my entire result disappears. If I try to do

word[i+1] = piglatin[0];
	word[i+2] =piglatin[1];


instead of what I have above that (using 'a' and 'y') it will also not display at all. Is there a specific reason for this? I feel like I'm close, as I'm getting the result I want apart from that extremely bizarre "H_AGENT_PID=1695" message.

EDIT: for clarity this is my exact result if I run and use "hello"

Original string is: hello
Pig Latin word is: ellohayH_AGENT_PID=1695

This post has been edited by whitaker144: 17 November 2013 - 03:44 PM

Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Trouble with pointer char modification and malloc

Posted 17 November 2013 - 07:57 PM

View Postwhitaker144, on 17 November 2013 - 10:40 PM, said:

I feel like I'm close, as I'm getting the result I want apart from that extremely bizarre "H_AGENT_PID=1695" message.

EDIT: for clarity this is my exact result if I run and use "hello"

Original string is: hello
Pig Latin word is: ellohayH_AGENT_PID=1695


Hi, a C string has a null character at the end. If a C string has the string "hello", the string length will be 5, but 6 elements will be required in the array/buffer to include the null character.

There are two basic things you are doing allocating memory and making the pig latin word. I don't like the idea of allocating memory in the function. I would just use the function to make the pig latin word.

and you could call it :

/* ... */
  else {
    char pig_latin_str[100];

    printf("Original string is: %s\n", argv[1]);
    PigLatin( pig_latin_str, argv[1] );
  }





You can use malloc later to create an array.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1