Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[SOLVED] Strings && Pointer Logic

c string

  • This topic is locked This topic is locked
4 replies to this topic

#1 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 03 November 2012 - 06:30 AM

I have an issue with the logics of C... if you could kindly fix my brain

See comments in code
here is my very simple code that does not work




void main(){

char test[] = "yes"; /* put a string into a char array...*/
char *ptr; /* Make a pointer of type char*/
ptr = test; /* pointer now points to the address of first character in test array*/
printf("works %s", *ptr[0]); /* print the first character that the pointer is pointing to */

}


I don't understand why this code does not work.. or how to make it work.. just want to break up the array test[] and print the first char test[0] and then test[1]

wont let me



thanks
  • 0
Your Friendly Neighborhood Pally

#2 macallik

macallik

    CC Newcomer

  • Validating
  • PipPip
  • 10 posts
  • Location:CT
  • Programming Language:C, Java, C++, Objective-C, C#, PHP, (Visual) Basic, Python, JavaScript, Visual Basic .NET, VBScript
  • Learning:Python, PL/SQL

Posted 03 November 2012 - 08:26 AM

If you are trying to print the first character you should use %c for character. %s is for a string.
  • 0

#3 Pally

Pally

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 413 posts
  • Programming Language:C, Java, PHP, Python, PL/SQL, Assembly
  • Learning:C++, C#, Lisp, Scheme

Posted 03 November 2012 - 08:47 AM

If you are trying to print the first character you should use %c for character. %s is for a string.


Thanks, its always the simple things that I overlook :/

solved
  • 0
Your Friendly Neighborhood Pally

#4 macallik

macallik

    CC Newcomer

  • Validating
  • PipPip
  • 10 posts
  • Location:CT
  • Programming Language:C, Java, C++, Objective-C, C#, PHP, (Visual) Basic, Python, JavaScript, Visual Basic .NET, VBScript
  • Learning:Python, PL/SQL

Posted 03 November 2012 - 09:50 AM

Your welcome. Same here, mostly the simplest things.
  • 0

#5 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 03 November 2012 - 05:39 PM

This topic has been marked as SOLVED. If you have a similar question or topic, you can go back to the subforum and start a new topic to continue discussions.
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: c, string

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download