help with simple question

This is a discussion on help with simple question within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> void search_array (char array[], char type) { if (strcmp ("nop", array) ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    help with simple question

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void search_array (char array[], char type)
    {
        if (strcmp ("nop", array) == 0){
            type = 'R';
            }else if (strcmp ("syscall", array) == 0){
                type = 'I';
        }
        printf("%c\n", type);
    }
    
    int main (void)
    {
        char type;
        char array[] = "syscall";
        search_array (array, type);
        printf ("%c\n", type);
        
    }
    I don't get why when I try to print value of type from the main it prints nothing, but when i print it from the function it prints the correct value.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because "type in search_array" and "type in main" are not the same variable and consequently don't have the same value.

  3. #3
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Which means you have to pass "type" by reference from main() to search_array().

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    cheers, all fixed now

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    83
    Quote Originally Posted by agentsmith View Post
    cheers, all fixed now
    Hai
    Here is your modified code
    Code:
    #include "stdafx.h"
    #include "stdio.h"
    // Change function.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void search_array (char array[], char *type)
    {
        if (strcmp ("nop", array) == 0){
            *type = 'R';
            }else if (strcmp ("syscall", array) == 0){
                *type = 'I';
        }
        printf("%c\n", *type);
    }
    
    int main (void)
    {
        char type;
        char array[] = "syscall";
        search_array (array, &type);
        printf ("%c\n", type);
    	return 0;
        
    }
    You are expecting modified value of local variable from called function..to achieve this
    you should go for either pass by address or pass by reference..but you are trying to use
    pass by value..both variables(Function & main) stored at different addresses..hence you
    will get garbage value when you print that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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