9 Replies - 5252 Views - Last Post: 22 April 2010 - 10:47 AM Rate Topic: -----

#1 Guest_eZACKe*


Reputation:

Array Notation to Pointer Notation

Posted 22 April 2010 - 08:42 AM

I have already written this program(for my class) using Array Notation, but we are asked to then write the same thing with Pointer Notation. This should be pretty easy because the two notations are interchangeable in C. I'm having trouble in a couple of places though:

In my main:

	char *s1 = "Brockport";
	char *t1 = "SUNY";

	printf("\n---In string Copy---\n");
	printf("Before string copy s = %s and t = %s\n",s1,t1);
	
	if(my_strcpy(s1,t1) == 1){
           
	    printf("String copied successfully\n");
	    printf("After string copy s = %s and t = %s\n",s1,t1);
	}
	
	else if(my_strcpy(s1,t1) == -1){
		
		printf("String not copied\n");
		printf("After string copy s = %s and t = %s\n",s1,t1);
	}
	
	else
		printf("\n Unknown error!\n");




So the main calls this function with those parameters and it goes here (where the problem occurs):

int my_strcpy(char *s, char *t)
{
	
      	//find the length of both strings
	int first_string = strlen(s);
	int second_string = strlen(t);

	//in this case the string will not be copied
	if(first_string < second_string)
	{
		return -1;
	}
	
	//Assign each positino in t the corresponding position in s
	for(int x = 0; x <= second_string; x++)
	{
	       *(s + x) = *(t + x);
	}
	
	//it works so return 1
		return 1;


}



When I run this code, this is the output:

[email protected]% ./zhine                              (~/workspace/csc311) 11:35 am


---In string Copy---
Before string copy s = Brockport and t = SUNY
zsh: segmentation fault (core dumped)  ./zhine





Is This A Good Question/Topic? 0

Replies To: Array Notation to Pointer Notation

#2 Guest_eZACKe*


Reputation:

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 08:44 AM

The same error is occurring in this function:

void my_strreverse(char *s)
{
	/*
	//a counter
	int count = 0;
	int l = strlen(s) - 1;//find the length of the string and set it to l

	//for the entire length of the string, use char swap to put the
	//last position in s, at the beginning of s
	while (count < l)
	{
		char swap = *(s+l);
		*(s+l) = *(s+count);
		*(s+count) = swap;

		++count;
		--l;
	}

	*/
	
}




Thank you!
Was This Post Helpful? 0

#3 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 08:54 AM

You can't modify a string literal. In fact the correct syntax (in C++) for the first 2 lines you posted would be
        const char *s1 = "Brockport";
        const char *t1 = "SUNY";


In C, it's just char *s1 == ... as you wrote it, but they are still treated as constants.

If you want to be able to modify them, you have to either define them as arrays:
        char s1[] = "Brockport";
        char t1[] = "SUNY";


and then you can still manipulate them using pointer notation as you are doing in your strcpy function.

Alternatively, you can declare the two pointers and use malloc to allocate memory to them, but then you have to copy the strings char-by-char into that memory -- you can't just initialize the dynamic memory with "=".

This post has been edited by r.stiltskin: 22 April 2010 - 08:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_eZACKe*


Reputation:

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 08:57 AM

Well the "Brockport" and "SUNY" lines in my main were written by the professor and we are not allowed to touch that code.

How can I make my code work while keeping it like that?
Was This Post Helpful? 0

#5 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 09:01 AM

Exactly what does your assignment say regarding the strcpy function?
Was This Post Helpful? 0
  • +
  • -

#6 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 09:07 AM

1. int my_strcpy(char s[], char t[]) – this function overwrites string s by a copy of characters in string t. The copy should succeed only if length of s is at least as great as t and the function should return a 1. If s is shorter than t then there should be no change to s and the function should return a -1

That's all it says. We then have to write in in Pointer notation.

This post has been edited by eZACKe: 22 April 2010 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 09:15 AM

s2 (the "source" string) can be a literal, but s1 (the "destination" string) MUST be a char array. There's no getting around that, so if you think that you are required to use those two string literals you should contact your professor.

If you have to convince him/her (or yourself) about this, here is a 3-line program that will prove it:

int main() {
  char *s1 = "hello";
  *s1 = 'w';
}


Try running that -- you can't, it is guaranteed to crash.
Was This Post Helpful? 1
  • +
  • -

#8 eZACKe   User is offline

  • Garbage Collector

Reputation: 120
  • View blog
  • Posts: 1,278
  • Joined: 01-June 09

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 09:26 AM

Thank you very much.
Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 09:27 AM

What you can do is use a char array as a temporary container for the destination string, and after the strcpy function is finished, you can assign the s1 pointer to point to the temporary array, like this:

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

int main() {
  char *s1 = "Brockport";
  char *t1 = "SUNY";

  char s3[strlen(s1) + 1]; // do you know why I made this "+ 1"?
  strcpy( s3, t1 );
  
  s1 = s3;

  printf("%s\n", s1);
}



or if your compiler won't allow that (because of the way I defined the array) since you know the length of s1 you can do this:
#include <stdio.h>
#include "your_string_header.h"

int main() {
  char *s1 = "Brockport";
  char *t1 = "SUNY";

  char s3[10]; // do you know why I made this array length 10?
  strcpy( s3, t1 );
  
  s1 = s3;

  printf("%s\n", s1);
}


This post has been edited by r.stiltskin: 22 April 2010 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#10 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Array Notation to Pointer Notation

Posted 22 April 2010 - 10:47 AM

Topic split. The original conversation would continue here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1