While loop to count the charecters of a string

This is a discussion on While loop to count the charecters of a string within the C Programming forums, part of the General Programming Boards category; Hello, What's wrong with: Code: int getLength(char* A){ int i = 0; while (*(A+i) != '/0') i++; return i; I ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95

    While loop to count the charecters of a string

    Hello,

    What's wrong with:

    Code:
    int getLength(char* A){
    	int i = 0;
    	while (*(A+i) != '/0') i++;
    	return i;
    I get an infinite loop at the while statement.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Seems unlikely. What's the rest of the code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Code:
    char A[] = "Alice was beginning to get tired of sitting by her sister on the bank, and of having nothing to do.";
    
    int getLength(char* A){
    	int i = 0;
    	while (*(A+i) != '/0') i++;
    	return i;
    }
    
    int main(){
    	printf("%d\n",getLength(*A));
    	return 0;

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, missed that you are using / instead of \ in what should be '\0'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    What about this:
    Code:
    void wrap(char* A, int m){
    	char* B = A;
    	int n = getLength(B);
    	int j,i;
    	for (i=m; i<n; i+=m){
    		j = i+1;
    		while (&(A+j) != " ") j--;  //error
    		&(A+j) = "\n"; //error
    	}
    }

  6. #6
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    actually there should be * follow the pointer, but that gives a warning too.

  7. #7
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    You are using double quotes when comparing to a char. A char is in single quotes.
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    why to mix pointer notation when you have index?

    Code:
    A[j] != ' '
    is a lot easier to read
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    I'm wondering why this code gives bad access signal:
    To me it should copy an array passed as A.

    Code:
    void costCalc(char* A, int m){
    	char* B;
    	int i=0;
    	/*while (*(A+i) != '\0'){ *(B+i) = *(A+i); i++;};
    	*(B+i) = '\0';

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This thread, ladies and gentlemen, is why you should compile with warnings on.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Location
    Bozen
    Posts
    95
    Why this:

    Code:
     
    void wrapper(char A[],int n, int m){
    	int i= 0;
    	while (A[i] != ' ' && A[i] != '\0' && i<n) i++; //so from 0 to i we have the first word assuming we never start with a space, in case of "aa" returns 2
    	A[i] = '\n';
    gives bad access signal when passed "aa" as char and n as 3?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How are you calling the function - most likely, you are writing to a place in memory that isn't writable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  2. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21