Thread: pointers on arrays

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    pointers on arrays

    Code:
    #include <stdio.h>
    int main(){
    char *str;
    char t;
    int i=0, j=0;
    printf("\n");
    printf("Enter String (up to 50 chars): ");
    scanf("%s", *str[j]);
    	for (j = 0; *str; ++j);
    
    	i==j;
    	j==0;
    
    	while(i>=j){
    	t = *str[i];
    	*str[i] = *str[j];
    	*str[j] = t;
    	i--;
    	j++;
    
    	}
    
    	printf("%s\n",(str));
    	
    
    printf("\n");
    }

    what i'm i doing wrong...

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Many, many things:
    • main returns an int, but you fail to do so.
    • char *str, is a pointer that you never actually allocated any space for, however you attempt to store items in it
    • You seem to be confusing a char array and a char in your loops
    • Your compiler should be having a fit over this code, I suggest you pay attention to it or get a new compiler

    You will benefit from reading through our C Tutorials. Yes, all of them; there are serious conceptual flaws with this code.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    What are u basically up to??? I don't understand where to start from....Okay lets give it a try to explain ur mistakes.....

    Code:
    char *str;
    str is a character pointer that can hold only 1 address which simply points to a character or group of characters(string). In this case, it holds the starting address of the input string.

    str is not an array. So *str[j] is not correct but str[j] may be correct. You can use this to swap characters in the string.

    Try,
    Code:
    scanf("%s",str);
    instead of
    Code:
    scanf("%s",*str[j]);
    Why do u give *str in the for loop's condition? *str means the 1st character of the input string, when its correctly formatted; otherwise its raw bits which makes no sense to the for loop. It probably crashes ur program.

    U can find the length of the string by strlen(str)

    == is a check for equality which evaluates to either 0 or 1. I think its =, that's what u need or searching for, may be.

    Again, the following is wrong as str is not an array. [You confused between pointer to a string and an array of null-terminated characters(string).]
    Code:
    t = *str[i];
    *str[i] = *str[j];
    *str[j] = t;
    *str[i] makes no sense.

    Dont get discouraged....u simply need a to read ur text book properly!!!
    Last edited by Avenger625; 09-11-2011 at 12:07 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Avenger625
    str is not an array. So str[j] is not correct.
    It is true that str is not an array, but depending on the value of j, str[j] may be correct. str[j] would be equivalent to *(str + j).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    Right!!!

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by antros48 View Post
    what i'm i doing wrong...
    Not telling us what the problem is, and not asking a question about how to fix it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by antros48 View Post
    what i'm i doing wrong...
    Although nobody has come straight out and said it... You need to allocate memory for your input string before you do anything else...
    You have two chioices here...
    Code:
    // choice #1
    char str[51];  
    
    //choice #2
    char *str = malloc (51 * sizeof(char));
    If you choose #2, you need to free() the allocated memory when you are done with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to pointers with arrays
    By Nurumla in forum C Programming
    Replies: 3
    Last Post: 07-18-2011, 11:53 PM
  2. Help with Arrays and pointers.
    By Nocturne in forum C Programming
    Replies: 2
    Last Post: 12-10-2010, 05:21 PM
  3. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. pointers & arrays
    By falconetti in forum C Programming
    Replies: 4
    Last Post: 12-18-2001, 11:55 PM