Is my computer haunted

This is a discussion on Is my computer haunted within the C Programming forums, part of the General Programming Boards category; I am trying to learn about pointers and how to point to an array and how to increment a pointer ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    Is my computer haunted

    I am trying to learn about pointers and how to point to an array and how to increment a pointer

    when i run this my output is: h,i,j,k,l,m

    but i have no h,i,j,k,l,m in my code?!! this does not make sense to me

    can anyone explain it?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    main()    {
    
    char* ptr;
    int n;
    
    
         
    char string1 [] = { 'A' , 'B' , 'C' , 'D' , 'E' , 'F', 'G', '\0' } ;
    
    n = 3;
    
    ptr = string1;
    
    
    
    while ( *ptr != string1[6] ){
    
          
    printf( "my pointer ptr points at: %c \n", ptr );
    
    ptr++ ;  }
    
    
    
    printf("%s",string1);
    
    getchar();
    
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    printf( "my pointer ptr points at: %c \n", ptr );
    This is saying to print out a character but you pass in a pointer.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    Code:
    while ( *ptr != string1[6] ){
    That's probably not what you want, unless the string never contains duplicate characters.

    main() should have a return 0.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    What's n for? You assign 3 to it, and that's the last and first time it's used.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 34
    Last Post: 02-26-2006, 12:16 PM
  2. Major Computer Problem
    By Olidivera in forum Tech Board
    Replies: 10
    Last Post: 07-15-2005, 11:15 AM
  3. Tabbed Windows with MDI?
    By willc0de4food in forum Windows Programming
    Replies: 25
    Last Post: 05-19-2005, 10:58 PM
  4. Computer will not boot.
    By RealityFusion in forum Tech Board
    Replies: 25
    Last Post: 09-10-2004, 04:05 PM
  5. This is my last night on this computer.
    By joshdick in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-12-2003, 01:33 AM

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