PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 17, 2022

[FIXED] How to convert char* argv[1] to int and print it without warning in C

 July 17, 2022     argv, c, char, int, warnings     No comments   

Issue

I want to convert the argv[1] to an int. But I get this warning:

xorcipher.c:7:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]

And the printf display me

-799362156

if I type

./xorcipher 4

How to correct this?

Here is my code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char* argv[])
{
    int key_length = atoi(argv[1]);
    printf("key_length = %d", &key_length);
    return(0);
}

Solution

You are passing the address of key_length, where as the error states, printf expects just the value. Try this:

printf("key_length = %d", key_length);

See this tutorial on C format specifiers.



Answered By - Nik
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing