Zugriffsverletzung bei einer for-Schleife



  • Guten Abend,
    ich arbeite mir Visual Studio 2015 und habe ein Problem mit einer Bibliothek die Zahlen aus einer Variable extrahieren soll.

    Der Code:

    sort.cpp

    #include "sort.h"
    
    bool Sort::IsInteger(char c)
    {
    	switch (c)
    	{
    	case 0:
    	case 1:
    	case 2:
    	case 3:
    	case 4:
    	case 5:
    	case 6:
    	case 7:
    	case 8:
    	case 9:
    		return true;
    		break;
    	default: 
    		return false;
    		break;
    	}
    }
    
    int Sort::WhatInteger(char c)
    {
    	switch (c)
    	{
    	case 0:
    		return 0;
    		break;
    	case 1:
    		return 1;
    		break;
    	case 2:
    		return 2;
    		break;
    	case 3:
    		return 3;
    		break;
    	case 4:
    		return 4;
    		break;
    	case 5:
    		return 5;
    		break;
    	case 6:
    		return 6;
    		break;
    	case 7:
    		return 7;
    		break;
    	case 8:
    		return 8;
    		break;
    	case 9:
    		return 9;
    		break;
    	default:
    		return 0;
    		break;
    	}
    }
    
    void Sort::SortCharToInteger(char* c_source, int* i_destination)
    {
    	char c_sort;
    	char* c_point;
    
    	c_point = 0;
    
    	for (int i = 0; c_source[i] != '\0'; i++)
    	{
    		c_point = &c_sort;
    		*c_point = c_source[i];
    
    		if (IsInteger(c_sort) == true)
    		{
    			i_destination += WhatInteger(c_sort);
    		}
    	}
    }
    

    sort.h

    #pragma once
    
    #include "stdafx.h"
    
    class Sort
    {
    public:
    	bool IsInteger(char c);
    	int WhatInteger(char c);
    	void SortCharToInteger(char* c_source, int* i_destination);
    
    private:
    };
    


  • Ich nehme an, c_source war nicht nullterminiert, also daß das Problem nicht in den hier gezeigten Zeilen liegt.



  • #include "sort.h"
    
    //Mal ASCII angenommen…
    bool Sort::IsInteger(char c)
    {
    	return '0'<=c and c<='9';
    }
    
    int Sort::WhatInteger(char c)
    {
    	return c-'0';
    }
    
    void Sort::SortCharToInteger(char* c_source, int* i_destination)
    {//besser wäre "int Sort::SortCharToInteger(char* c_source)"
    	*i_destination=0;
    	for (char pc=c_source; *pc!='\0'; ++*pc)
    		if (IsInteger(*pc))
    			*i_destination = *i_destination*10+WhatInteger(*pc);
    }
    


  • volkard schrieb:

    {//besser wäre "int Sort::SortCharToInteger(const char* c_source)"
    

    FTFY 😉


Anmelden zum Antworten