LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-20-2012, 07:09 PM   #1
suttiwit
Member
 
Registered: Aug 2012
Location: Chiang Mai, Thailand
Distribution: Kubuntu 12.10 x86_64
Posts: 192
Blog Entries: 2

Rep: Reputation: 23
Post gcc is noughty on my c code.


Hello all..
I am making a code that prints random characters from the template.
Thing is.. I make a printf() and specify template[ch]. when 'template' is a char and 'ch' is an int to specify where in the char it will print.
But gcc does not look at template. it looks at 'ch' and thought it was int.

I don't know who's fault this is (gcc or me). Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
        char template[999] = "qwertyuiopasdfghjklzxcvbnm1234567890äåéëþüúíóö«»áßðø¶æ©®bñµç.¿¹²³¤^¥×¡,~_£¼½¾,~*÷ÄÅÉËÞÜÚÍÓÖÁ§ÐÏØÆ¢°®ÑµÇ";
        int ch;
        for (;;)
        {
                ch = rand() % strlen(template);
                printf("%s", template[ch]);
                fflush(stdout);
        }
        return 0;
}
I need it soon! THanks in advance.
 
Old 11-20-2012, 07:42 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by suttiwit View Post
Hello all..
I am making a code that prints random characters from the template.
Thing is.. I make a printf() and specify template[ch]. when 'template' is a char and 'ch' is an int to specify where in the char it will print.
But gcc does not look at template. it looks at 'ch' and thought it was int.

I don't know who's fault this is (gcc or me). Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
        char template[999] = "qwertyuiopasdfghjklzxcvbnm1234567890äåéëþüúíóö«»áßðø¶æ©®bñµç.¿¹²³¤^¥×¡,~_£¼½¾,~*÷ÄÅÉËÞÜÚÍÓÖÁ§ÐÏØÆ¢°®ÑµÇ";
        int ch;
        for (;;)
        {
                ch = rand() % strlen(template);
                printf("%s", template[ch]);
                fflush(stdout);
        }
        return 0;
}
I need it soon! THanks in advance.
Add '-Wall -Wextra' to your compilation command line and make sure there are zero warnings. You might need also '-O2' to activate yet more warnings, or consult 'gcc' documentation to see how 'printf' format string related warnings are activated.

I am telling you that you have type mismatch, and it's your challenge to understand why/where.
 
Old 11-20-2012, 08:31 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,599
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
"%s" expects a pointer to a null-delimited string of characters.

But template[ch] is not that: it's a single (integer...) value consisting of one character taken from an array.

No wonder "C" is confused. The ASCII equivalent of, say, the letter "q", is probably not a valid pointer!
 
1 members found this post helpful.
Old 11-21-2012, 01:16 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It is even worse than that. The value of template[ch] will always be the terminating null byte of the initializing literal string. So dereferencing it should result in a segmentation fault.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc unreachable code PatrickNew Programming 26 02-26-2012 12:49 PM
how to compile (gcc) using code from other projects Meson Programming 5 01-18-2010 01:07 AM
porting code to gcc ..template code problems aayudh Programming 1 01-17-2009 03:40 PM
excluding code from gcc optimization gearoid_murphy Programming 7 03-12-2008 05:09 AM
gcc/Assembly code gigantas1985 Programming 0 02-28-2006 05:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration